我试图使用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>
发布于 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
发布于 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
:,WebDriverWaitbutton(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
https://stackoverflow.com/questions/51252089
复制相似问题