使用seleniumpython单击一个div

2024-09-27 19:19:07 发布

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

网站是:https://www.smile.one/merchant/mobilelegends?source=other

如果单击下图中的任何项目,您将在该选择上获得一个绿色勾号。请参见这两幅图

enter image description here

选择后:

enter image description here

我无法使用selenium执行此单击操作。

这就是我正在尝试的:

driver.get("https://www.smile.one/merchant/mobilelegends?source=other")
driver.find_element_by_xpath("//div[@class='sectionNav-cartao n107002' and @ga-data='1717']").click()

这引发了一个例外:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

我还尝试使用inspect元素使用精确的直接路径:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body > mate > div.main-container > section > div.section-nav > div.sectionNav-list > div.sectionNav-cartao.n107002"))).click()

并得到一个timeout错误

我也试过其他几种方法,但似乎都不管用
如何执行此单击


Tags: httpsdivsourcewwwdriverseleniummerchantelement
1条回答
网友
1楼 · 发布于 2024-09-27 19:19:07

如果要使用XPath = "//div[@class='sectionNav-cartao n107002' and @ga-data='1717']",则有两个元素具有相同的XPath。使用像XPath = "//div[@class='pc-nav']//div[@class='sectionNav-cartao n107002' and @ga-data='1717']"这样的唯一XPath,它在chrome开发工具中只突出显示一个元素

代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.smile.one/merchant/mobilelegends?source=other")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@class='pc-nav']")))

element = driver.find_element(By.XPATH, "//div[@class='pc-nav']//div[@class='sectionNav-cartao n107002' and @ga-data='1717']").click()

相关问题 更多 >

    热门问题