Reputation: 976
The code below works fine with Firefox, but when I set browser=webdriver.Chrome() I get some issues.
browser = webdriver.Firefox()
browser.get('https://www.voilanorbert.com/')
inputElement = browser.find_element_by_id("form-search-name")
inputElement.send_keys(leadslist[i][0])
inputElement = browser.find_element_by_id("form-search-domain")
inputElement.send_keys(leadslist[i][1])
searchbutton = browser.find_element_by_name("search")
searchbutton.click()
wait = WebDriverWait(browser, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results")))
h3s = browser.find_elements_by_css_selector('h3.one')
h3 = next(element for element in h3s if element.is_displayed())
result = h3.text
With chrome, everything is fine up until I need to extract text. I get this error:
h3 = next(element for element in h3s if element.is_displayed())
StopIteration
EDIT
Problem showing up again on both firefox and chrome. I even added a fluent wait.
wait = WebDriverWait(browser, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results")))
wait2 = WebDriverWait(browser, 3000, poll_frequency=100, ignored_exceptions=[ElementNotVisibleException])
wait2.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "h3.one")))
h3s = browser.find_elements_by_css_selector('h3.one')
h3 = next(element for element in h3s if element.is_displayed())
result = h3.text
I have came to the conclusion that selenium is definitely not perfect.
Upvotes: 1
Views: 1031
Reputation: 1021
If it is a wait issue I've had good experience with fluent wait:
https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html
Upvotes: 1
Reputation: 474171
I suspect the key problem here is in the animation happening when the results are shown.
You need to let selenium
know that you want it to wait before searching for an element via implicitly_wait()
:
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results")))
# okay, selenium, slow down, please
browser.implicitly_wait(3)
h3s = browser.find_elements_by_css_selector('h3.one')
(worked for me)
Upvotes: 1