这是 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>
从上面的代码片段来看,它是一个卡片元素,并且使用 selenium python 我尝试单击该元素。
我的代码是:
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)
最佳答案
您可能需要等待该元素变得可点击,然后再单击它。使用 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)
关于javascript - 我应该选择什么元素值才能单击该元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56354318/