【发布时间】:2021-09-13 04:27:41
【问题描述】:
我正在尝试使用Selenium 和xpath 单击网页中的按钮。我设法点击了上一页中的一个按钮,但是在加载到这个新页面后,我尝试使用相同的代码但不同的按钮 HTML,它无法像以前那样加载。
我的代码如下:
driver = webdriver.Chrome()
driver.get("https://connect.com")
driver.find_element_by_xpath("//button[@class='p-button p-component']").click() #button workable
In the next page:
driver.find_element_by_xpath("//button[@class='add icon-only proto-button ng-star-inserted']").click() #button not working
我收到的错误信息是:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
我也尝试过如下添加等待时间,但还是不行。
button = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, "//button[@class='add icon-only proto-button ng-star-inserted']")))
button.click()
但是,现在我收到的错误信息是:
selenium.common.exceptions.TimeoutException: Message:
例如,我也尝试过处理 HTML 代码的其他元素。但无论哪种方式,按钮都不起作用。
driver.find_element_by_xpath("//span[text()='proto-icon ng-star-inserted']").click()
or
driver.find_element_by_xpath("//connect-button[@class='proto-button-list-item ng-star-inserted']").click()
The HTML is in the picture attached
我不知道我还能如何解决。请指教,谢谢!
【问题讨论】:
-
我猜
/button被/connect-button覆盖,所以它不可点击。你试过用 JSclick 点击它吗? -
嗨@lucasnguyen17,我对Python和Selenium真的很陌生,我不太确定该怎么做。你能大致告诉我它的样子吗?是的,从 HTML 中可以看到 /connect-button 在 /button 元素上方。
-
好的,我想我需要凭据才能在这里登录 brandforce.com/domain/connect.com ?如果是这样,我没有它们。请分享您要点击的按钮的 HTML?
-
@ejbeh
element = driver.find_element_by_xpath("//button[@class='p-button p-component']") driver.execute_script("arguments[0].click();", element) -
感谢@lucasnguyen17 我已经尝试使用
element = driver.find_element_by_xpath("//button[@class='add icon-only proto-button ng-star-inserted']")并且它有效
标签: python selenium button xpath