Selenium Python - Test if an element exists in the page -
I'm new to Selenium and I would like to write a Python script that searches for some keywords on Google and automatically The page opens and the keywords are found.
I'm trying to work with the script:
Selenium from Selenium Import WebDriver Com.exceptions Import timeout selenium from Akspshn selenium.webdriver.support.ui import webdriver.support.ui import WebDriverWait selenium.webdriver.common.by select Import selenium.webdriver as EC selenium.common.exceptions. imports NoSuchElementException by the support import expected_conditions, then __name__ == "__main__": driver = webdriver.Firefox () wait = WebDriverWait (driver, 100) driver.get ( "http://google.com") inputElement = driver .find_element_by_name ( "q") inputElement.send_keys ( "Irwin Quan") wait.until (EC.element_to_be_clickable ((By.XPATH, "//a[@href='http://irwinhkwan.wordpress.com/ '] "))) Blog = driver.find_element_by_xpat H (" //a[@href='http://irwinhkwan.wordpress.com/ '] ") blog .click () driver.quit () This works perfectly in the Xpath page, though, if it is not, we always wait for it.
How can I check that an element exists, and if it is there, then I click on it?
I tried "" as it has been given in the documentation, or NoSuchElementException , but I "dual deny" - [try: EC driver = webdriver.firefox () Import from imported_conditions to selenium.webdriver.support.ui as driver.get ("// somedomain / url_that_delays_loading http") Import WebDriverWait by webdriver.common.by: element = WebDriverWait (driver, 10) .until (EC.presence_of_element_located ((By.ID, "myDynamicElement"))) finally: driver.quit () finally, I would like a while loop, which results in Google The page number goes to x and every Salvation is to find a specific address.
If someone can give me a hand which will be great! I'm pretty sure that this is trivial, but despite all the documents available availab online I have not been able to do this work for the past few days ...
= "post- Text "itemprop =" text "> Here is a little cover for WebAlement I, I've written, hope that it helps
class element (object): def __init __ (self, xpath = none): self.xpath = xpath def __get_element (self):. Return driver () find_element_by_xpath (self.xpath) def is_exist (self): try: return self .__ NoSuchElementException, e except get_element () is_displayed () :. Logger.exception (e.message) false return You can not click on the element that is not visible, first you need to expose it, and then only after Click
Hope it helps
Comments
Post a Comment