Issue
I need to click every item in dropdown list from this page and I write this code for click every items in dropdown list but I am getting error element is not attached to the page document
after click on few items.
here is my code:
option_variations = driver.find_element_by_css_selector('#sid')
if option_variations:
options = [x for x in option_variations.find_elements_by_tag_name("option")]
for i in options:
variation = i.text
click_item = driver.find_element_by_xpath(f"//*[contains(text(), '{variation}')]")
click_item.click()
print(f"sucessfully click on item {variation}")
time.sleep(3)
Solution
The following code should work:
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.common.by import By
import chromedriver_autoinstaller
from selenium.webdriver.chrome.service import Service
import time
chromedriver_autoinstaller.install()
driver = webdriver.Chrome(service=Service())
# open page
driver.get("https://www.zzounds.com/item--PGHPHM")
# get the variations into an array
elements = driver.find_elements(by=By.CSS_SELECTOR, value="#sid option")
values = []
for element in elements:
value = element.get_attribute("value")
values.append(value)
print(values)
# click on each variation
for value in values:
click_item = driver.find_element(by=By.CSS_SELECTOR, value=f"#sid option[value='{value}']")
click_item.click()
print(f"sucessfully click on item {value}")
time.sleep(1)
The problem what the xpath selector itself for each variant was not always valid and you must start by creating a simple list of strings for each option.
Answered By - doberkofler
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.