简体   繁体   English

无法使用 python selenium 与此动态下拉列表交互

[英]unable to interact with this dynamic drop down using python selenium

网站截图

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 作为标签访问动态下拉菜单,但我能够找到它但无法与其交互,因为它会更改其样式类型,如下所示。

<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,您必须单击元素才能访问此动态下拉列表的信息,

div is not clickable object in HTML . HTML 中的div不可点击HTML If it has assigned some JavaScript code to display when you click it then you may need also JavaScript to click it如果它已分配一些JavaScript代码以在您单击它时显示,那么您可能还需要JavaScript才能单击它

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

and the same way you can change style和你改变style的方式一样

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

In this minimal working example I remove all img on this page.在这个最小的工作示例中,我删除了此页面上的所有img

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)

Solution -> actually the element we are looking here is masked element which means actual Id of this element is diff so by chance I am able to find it( for this you have go through HTML code row by row and find it, quiet a static way to do but that's how I did it) and place it in the code.解决方案 -> 实际上我们在这里寻找的元素是屏蔽元素,这意味着这个元素的实际 ID 是差异的,所以我偶然能够找到它(为此你有 go 到 HTML 代码逐行找到它,安静一个 static方法,但我就是这样做的)并将其放入代码中。

please do comment if you know more efficient way to work around masked elements.如果您知道更有效的解决屏蔽元素的方法,请发表评论。

Regards, Anubhav问候, 阿努巴夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM