簡體   English   中英

無法使用 python selenium 與此動態下拉列表交互

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

網站截圖

大家好,

我正在嘗試使用 div 作為標簽訪問動態下拉菜單,但我能夠找到它但無法與其交互,因為它會更改其樣式類型,如下所示。

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

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

我無法點擊它,請查看屏幕截圖了解詳情。

您必須單擊元素才能訪問此動態下拉列表的信息,

HTML 中的div不可點擊HTML 如果它已分配一些JavaScript代碼以在您單擊它時顯示,那么您可能還需要JavaScript才能單擊它

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

和你改變style的方式一樣

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

在這個最小的工作示例中,我刪除了此頁面上的所有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)

解決方案 -> 實際上我們在這里尋找的元素是屏蔽元素,這意味着這個元素的實際 ID 是差異的,所以我偶然能夠找到它(為此你有 go 到 HTML 代碼逐行找到它,安靜一個 static方法,但我就是這樣做的)並將其放入代碼中。

如果您知道更有效的解決屏蔽元素的方法,請發表評論。

問候, 阿努巴夫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM