Right now my script go to page and open the second object from the drop down list, "Vijesti", before I get the error message.
This is the error:
StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
From Selenium site:
Thrown when a reference to an element is now “stale”. Stale means the element no longer appears on the DOM of the page. Possible causes of StaleElementReferenceException include, but not limited to:
- You are no longer on the same page, or the page may have refreshed since the element was located.
- The element may have been removed and re-added to the screen, since it was located. Such as an element being relocated. This can happen typically with a javascript framework when values are updated and the node is rebuilt.
- Element may have been inside an iframe or another context which was refreshed.
What I want to select each object, and open it.
This is the SELECT part from the url:
<select id="kategorija" name="kategorija">
<option value="0">Kategorija</option>
<option value="12">Vijesti</option>
<option value="8">Biznis</option>
<option value="5">Sport</option>
<option value="2">Magazin</option>
<option value="7">Lifestyle</option>
<option value="3">Scitech</option>
<option value="6">Auto</option>
</select>
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Firefox()
driver.get("http://www.klix.ba/")
assert "Klix" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("test")
elem.send_keys(Keys.RETURN)
select = Select(driver.find_element_by_name('kategorija'))
all_options = [o.get_attribute('value') for o in select.options]
for x in all_options:
select.select_by_value(x)
time.sleep(3)
This is the url where I do my testings.
is_selected() element method – Selenium Pythonis_selected method is used to check if element is selected or not. It returns a boolean value True or False.It can be used to check if a checkbox or radio button is selected.
The Select Class in Selenium is a method used to implement the HTML SELECT tag. The html select tag provides helper methods to select and deselect the elements. The Select class is an ordinary class so New keyword is used to create its object and it specifies the web element location.
The page refreshes itself when an item is chosen from the dropdown.
You need to "refind" the select
element on each option select:
select = Select(driver.find_element_by_name('kategorija'))
for index in range(len(select.options)):
select = Select(driver.find_element_by_name('kategorija'))
select.select_by_index(index)
# grab the results
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With