我有一个包含以下内容的网页:
<a id="show_more" class="friends_more" onclick="Friends.showMore(-1)"
style="display: block;">
<span class="_label">Show More Friends</span><span class="friends_more_icon"></span>
</a>
我想在 Python 3.5 中使用 Chrome 浏览器中的 Selenium 单击此链接。如果我使用 WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, id_text)))
然后继续点击它,没有任何反应。
但是,如果我在 WebDriverWait
返回可点击元素后等待 - 我可以点击它。
我尝试了两种点击链接的方法:
more_friends_link = wait_for_id("show_more", driver)
ActionChains(driver).move_to_element(more_friends_link).click(more_friends_link).perform()
和
more_friends_link = wait_for_id("show_more", driver)
more_friends_link.click()
如果在 WebDriverWait
返回元素之后再等待,这两种方法都可以正常工作。我不介意等待,但我想弄清楚为什么会发生这种情况,因为没有一个点击方法会引发异常,这让我认为该元素存在、可见且可点击,但它不响应点击。
UPD:我忘了说脚本在站点更改设计之前工作正常。以前可以通过链接文本找到链接,点击也能正常工作。
UPD2:所以目前我的代码如下所示:
more_friends_link = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "show_more")))
time.sleep(5)
more_friends_link.click()
我的目标是用更有意义的东西替换 time.sleep(5)
最佳答案
尝试替换
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, id_text)))
与
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, id_text)))
关于javascript - 等待链接点击 Selenium 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38282708/