Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

unable to interact with this dynamic drop down using python selenium

website screenshot

hi team,

I am trying to access a dynamic drop down with div as tag but I an able find it but not interact with it as it changes its style type as shown below.

<div style ="display :none;"></div>"

to

<div style ="display :block;"></div>"

I am unable to click on this, please have a look into the screenshot for detail.

Info you have to click on the element to access this dynamic dropdown,

question from:https://stackoverflow.com/questions/65931008/unable-to-interact-with-this-dynamic-drop-down-using-python-selenium

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

div is not clickable object in HTML. If it has assigned some JavaScript code to display when you click it then you may need also JavaScript to click it

driver.execute_script("arguments[0].click()", item)

and the same way you can change style

driver.execute_script("arguments[0].style.display = 'block';", item)

In this minimal working example I remove all img on this page.

from selenium import webdriver
             
url = 'https://stackoverflow.com/questions/65931008/unable-to-interact-with-this-dynamic-drop-down-using-python-selenium'

driver = webdriver.Firefox()
driver.get(url)

all_items = driver.find_elements_by_xpath('//img')
for item in all_items:
    print(item.text)
    #driver.execute_script("arguments[0].click()", item)
    driver.execute_script("arguments[0].style.display = 'none';", item)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...