javascript - 等待链接点击 Selenium 的正确方法

标签 javascript python python-3.x selenium

我有一个包含以下内容的网页:

<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/

相关文章:

php - Uncaught ReferenceError : add_chamber is not defined

python - 属性错误 : 'NoneType' object has no attribute 'get' - Python - OpenErp

python-3.x - Dask Dataframe 查看整行

python - 在 Python 2 和 3 中获得相同的 Unicode 字符串长度?

javascript - 猎犬.js : Transform the data returned by a remote source?

javascript - graphQl - 参数具有无效值预期\"contentfulStranNaslovQueryString_2\",发现不是对象

python - 如何转换和保存文件(os.walk)

python - 将颜色打印到 python 命令行

python - Dict of Dict 到 CSV(带有已定义的 header )

javascript - 用于多个光源的 WebGL 片段着色器?