1

I have some selenium code to input various search terms into a website's search field using the following code:

browser = webdriver.Chrome()
browser.get(url)

search_box1 = browser.find_element_by_id('searchText-0')
search_box2 = browser.find_element_by_id('searchText-2-dateInput')

search_box1.send_keys("Foobars")
search_box1.send_keys("2013")

search_box1.submit()

and then I have more code written to grab the number of hits that result from the given search query. However, for some values of "Foobars" in particular years, there are no hits and the query results in a page like this:

<body class="search">
    <div id="skip">...</div>
    <div style = "display:non;">...</div>
    <div id="container" class="js">
        <div id="header">...</div>
        <div id="search">...</div>
        <div id="helpContent">...</div>
        <div id="main-body" class="noBg">
           <div class="error">
              <div>Sorry. There are no articles that contain all the keywords you entered.</div>
              <p>Possible reasons:
              </p>
              <ul></ul>
              <p></p>
              <p></p>
          </div>
        </div>

How can I check that the search query is this rather than the page I get when there are hits from the search query? I was going to implement an if statement to check that the search query returned something, but I can't seem to figure out the right syntax to get the error element I need to do this. I've tried things like:

Error=browser.find_element_by_name('error')

or

Error=browser.find_element_by_xpath("//div[@class='error']")

But I keep getting the error:

selenium.common.exceptions.NoSuchElementException: Message: u'no such element\n

I want to identify the error element so I can do something like

if Error == "There are no articles that contain all the keywords you entered":
      do something
else:
      do something else

or even better, something that will tell me if the error exists to use for the conditional. Any help would be much appreciated.

2 Answers 2

1

Perhaps you are getting there too fast? Try

from selenium.webdriver.support import expected_conditions as EC
....
Error= WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@class='error']")))
Sign up to request clarification or add additional context in comments.

Comments

0

I found a solution that seems to work pretty well. You need to import the selenium common exceptions module then use try/execept. In the example code I am navigating to a page given by url and inputing Foobars and 2013 into two search fields. Then, I am recovering the number of hits that result, which is stored in navBreadcrumb.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

browser = webdriver.Chrome()
browser.get(url)

search_box1 = browser.find_element_by_id('searchText-0')
search_box2 = browser.find_element_by_id('searchText-2-dateInput')

search_box1.send_keys("Foobars")
search_box1.send_keys("2013")

search_box1.submit()

try:
    Hits = browser.find_element_by_id('navBreadcrumb').text
    Hits = int(Hits)
except NoSuchElementException:
    Hits = int(0)

browser.quit()

Comments

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.