Python Selenium用innterHTML查找Button并单击它

时间:2019-05-03 23:10:31

标签: python selenium

我试图找到以下按钮并单击它:

a.h

我已经尝试找到具有以下内容的按钮:

<div id="subText" class="btn btn-success">Subscribe</div>

但出现以下错误:

driver.find_element_by_css_selector('div.btn.btn-success').click()

是否可以仅搜索带有“订阅”文本的按钮并单击它?


编辑: 我发现以下元素阻止了按钮的点击:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div id="subText" class="btn btn-success">...</div> is not clickable at point (408, 513). Other element would receive the click: <div class="delete-overlay white" style="">...</div>

有没有办法等待/停止脚本,直到元素/脚本消失并继续?

我还试图等到“订阅”按钮可见,但是那没有用:

<svg class="svg-inline--fa fa-spinner-third fa-w-16 fa-spin fa-4x" aria-hidden="true" data-prefix="fas" data-icon="spinner-third" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M456.433 371.72l-27.79-16.045c-7.192-4.152-10.052-13.136-6.487-20.636 25.82-54.328 23.566-118.602-6.768-171.03-30.265-52.529-84.802-86.621-144.76-91.424C262.35 71.922 256 64.953 256 56.649V24.56c0-9.31 7.916-16.609 17.204-15.96 81.795 5.717 156.412 51.902 197.611 123.408 41.301 71.385 43.99 159.096 8.042 232.792-4.082 8.369-14.361 11.575-22.424 6.92z"></path></svg>

1 个答案:

答案 0 :(得分:0)

根据消息<div class="delete-overlay white" style="">...</div>重叠,因此脚本无法单击<div id="subText" class="btn btn-success">Subscribe</div>按钮。

以下是处理这种情况的选项。

1)使用JavaScript:

ele = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "'div.btn.btn-success")))
driver.execute_script("arugments[0].click()",ele)

2)滚动到元素

ele = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "'div.btn.btn-success")))
ele.location_once_scrolled_into_view # this will scroll to the element
ele.click()