首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过Selenium和Python按照HTML点击引导程序按钮

如何通过Selenium和Python按照HTML点击引导程序按钮
EN

Stack Overflow用户
提问于 2018-07-10 02:40:10
回答 2查看 2.3K关注 0票数 3

我试图使用selenium脚本点击网页上的一个按钮,但它在使用下面这行代码时给出了以下错误:

driver.find_element_by_class_name('btn-primary').click()

错误信息如下:

ElementNotInteractableException: Message: Element <button class="btn-primary btn-text  sort-filter-clear-button" type="button"> could not be scrolled into view

按钮元素的HTML:

<button type="submit" class="btn-primary btn-action bookButton" id="bookButton" data-track="FLT.RD.Book.Bottom"><span class="btn-label">Continue Booking</span></button>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-10 02:46:22

尝试等待元素:

button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "bookButton")))
button.click()

它将等待至少10秒,直到元素可被单击。

注意:您必须添加一些导出:

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

编辑:您也可以像这样尝试使用js executor:

button = driver.find_element_by_id("bookButton")
driver.execute_script("arguments[0].click();", button)

如果你的按钮在一个iframe/frame中,首先你必须切换到这个frame,然后你才能和这个元素交互:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME("frame_name"))))
# do your stuff
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "bookButton")))
button.click()
driver.switch_to.default_content() # switch back to default content
票数 3
EN

Stack Overflow用户

发布于 2018-07-10 03:12:32

根据您共享的HTML语言和您提到的bootstrap按钮,当您尝试在所需元素上调用click()时,您需要引入WebDriverWait以使元素可单击,并且您可以使用以下解决方案之一:

  • CSS_SELECTOR

驱动程序( 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button.btn-primary.btn-action.bookButton#bookButton>span.btn-label"))).click()

  • XPATH:,WebDriverWait

button(driver,20).until(EC.element_to_be_clickable((By.XPATH,“//button@WebDriverWait=‘btn-主btn-动作bookButton’和@id='bookButton'/span@class='btn-label'"))).click()

注意::您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51252089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档