硒无法单击ajax按钮

时间:2020-04-15 14:12:55

标签: python selenium

我有一个像这样的按钮

<button data-control-name="more_comments" id="ember1181" class="comments-comments-list__load-more-comments-button artdeco-button artdeco-button--muted artdeco-button--1 artdeco-button--tertiary ember-view" style="" xpath="1"><!---->
<span class="artdeco-button__text" style="">
    Load more comments
</span></button>

并加载了ajax。

我的代码的最后一个代码是

browser.implicitly_wait(50)
browser.execute_script("document.getElementsByClassName('comments-comments-list__load-more-comments-button artdeco-button artdeco-button--muted artdeco-button--1 artdeco-button--tertiary ember-view')[1].click();")

我正在弄错这个错误了

selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'scrollIntoView' of undefined

我尝试过

implicit_wait

WebElementWait

selection by css selector

by xpath

但仍无改善

预先感谢

1 个答案:

答案 0 :(得分:0)

因此ID是动态的,因此您可以使用以下解决方案来处理您网页上的第一条评论部分:

 wait = WebDriverWait(driver, 10)
 wait.until(EC.element_to_be_clickable((By.XPATH, "/(//button[@data-control-name='comment'])[1]"))).click()
 wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Load more comments']"))).click()

注意::请在您的解决方案中添加以下内容

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