我应该选择什么元素值才能单击元素

2024-04-27 02:30:45 发布

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

以下是html代码片段:

<div autoid="" buttondecorator="" class="ideas-card _ngcontent-zty-5 collapsed-card" aria-label="Find new keywords" aria-expanded="false" aria-describedby="F7C56A74-5C67-4B02-B3E2-AFD3DFBA318B--0" aria-hidden="false" id="F49F12F8-6C5C-4567-AAD9-6C76A52FD801--0" tabindex="0" role="button" aria-disabled="false"></div>

从上面的代码片段来看,它是一个card元素,我正在使用seleniumpython尝试单击该元素。你知道吗

我的代码是:

find_new = browser.find_element_by_xpath("//div[@aria-label='Find new keywords']").click()

有没有其他方法来选择那个元素?你知道吗

我收到的错误消息是:

element not interactable
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.16299 x86_64)

Tags: 代码divinfofalse元素newhtmlelement
1条回答
网友
1楼 · 发布于 2024-04-27 02:30:45

您可能需要等待该元素成为可单击的,然后再单击它。使用WebDriverWait和预期条件:

from selenium.webdriver.support import expected_conditions

find_new = WebDriverWait(driver, 20).until(
 expected_conditions.presence_of_element_located((By.XPATH, "//div[@aria-label='Find new keywords']")).click()

您可以尝试的另一个技巧是使用JavaScript Executor:

find_new = browser.find_element_by_xpath("//div[@aria-label='Find new keywords']")
browser.execute_script("arguments[0].click();", find_new)

相关问题 更多 >