1

I am new to Selenium and I would like to write a Python script that searches some keywords on google and automatically opens a page when the keywords are found.

I have been trying to work around with the script from this website:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

if __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 Kwan")

    wait.until(EC.element_to_be_clickable((By.XPATH,"//a[@href='http://irwinhkwan.wordpress.com/']")))
    blog = driver.find_element_by_xpath("//a[@href='http://irwinhkwan.wordpress.com/']")
    blog.click()

    driver.quit()

It works perfectly if the xpath is displayed in the page. However, if it is not, we wait forever.

How can I check if an element is there, and if it is there I click on it?

I tried using 'try' as it is given in the documentation, or NoSuchElementException, but I admit being confused with the "double negation ;-):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

Ultimately, I would like a while loop that goes to the page number X of the google results and click each time it finds a specific address.

If someone can give me a hand that would be great! I am pretty sure it is trivial, but despite all the documentation available online I haven't been able to work this out for the last couple of days...

4
  • In your second block you're not really finishing the try/exception. If you include the except NoSuchElementException: print "Element was not found" before the finally: line your program will open the page, wait 10 seconds for the element to appear, allowing you to interact with it, or it won't find it within 10 secs, will print out the message. Either way it will close the browser at the end. Commented Jul 25, 2014 at 8:21
  • Thanks for your answer. Is NoSuchElementException' like and 'else if'? if I put except NoSuchElementException: print "Element was not found", it always prints that even though the link is on the page. What I want: 1) try to find the link in max. 10s 2) If it is found, then click on it or 3) if it cannot be found, driver.quit(). Commented Jul 25, 2014 at 8:31
  • Use of an exception catch like that can be considered and if/else I guess. Try this, if it works do this, else do something else other than blowing up. If you're always seeing the print line, perhaps your selector is incorrect? Commented Jul 25, 2014 at 8:52
  • @MarkRowlands I have written an answer to show you the code I tried Commented Jul 25, 2014 at 9:34

1 Answer 1

1

here is a little wrapper for WebElement I’ve written, hope 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.__get_element().is_displayed()
        except NoSuchElementException, e:
            logger.exception(e.message)
            return False

you cant click an element that is not visible, first you need to expose it, and then only click.

hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I am gonna try that now :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.