无法使用selenium单击div按钮

2024-04-26 21:43:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想点击一个按钮,实际上是一个div标签。我无法点击它。
from selenium import webdriver

url = "https://www.qoo10.sg/item/LAPTOP-SCREEN-PROTECTOR-SCREEN-GUARD-FOR-13-14-15-INCHES-2ND/410235318"

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

elem = driver.find_element_by_class_name('selectArea').click()

当我运行这个程序时,我发现这个错误

selenium.common.exceptions.ElementNotInteractableException: Message: Element <div id="ship_to_outer" class="selectArea"> could not be scrolled into view.

Tags: fromhttpsimportdivurlwwwdriverselenium
2条回答

有4个按钮具有相同的类名"ship_to_outer"-第一个按钮是隐藏的,因此您不能单击它。改为尝试下面的代码

driver.find_element_by_xpath('//div[@class="selectArea" and not(@id="ship_to_outer")]').click()

这张截图告诉我们:

img

有4个元素的类名为selectArea。第一个是看不见的。这就是为什么你会得到:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <div id="ship_to_outer" class="selectArea"> could not be scrolled into view.

所以,首先你必须指定你想要点击的元素。例如,第一个下拉列表:

img2

有身份证,可以这样找到:

driver.find_element_by_id('inventory_outer_0').click()

相关问题 更多 >