Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Selenium和Python按照HTML单击引导按钮_Python_Selenium_Selenium Webdriver_Xpath_Css Selectors - Fatal编程技术网

如何通过Selenium和Python按照HTML单击引导按钮

如何通过Selenium和Python按照HTML单击引导按钮,python,selenium,selenium-webdriver,xpath,css-selectors,Python,Selenium,Selenium Webdriver,Xpath,Css Selectors,我试图使用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 s

我试图使用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
ElementNotInteractiableException:消息:无法将元素滚动到视图中
按钮元素的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>
继续预订

尝试等待元素:

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
根据您共享的HTML以及您提到的引导按钮,当您试图在所需元素上调用
click()
时,您需要引导WebDriverWait使该元素可单击,并且您可以使用以下任一解决方案:

  • CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-primary.btn-action.bookButton#bookButton>span.btn-label"))).click()
    
  • XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn-primary btn-action bookButton' and @id='bookButton']/span[@class='btn-label'][contains(.,'Continue Booking')]"))).click()
    
注意:您必须添加以下导入:

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

虐待?我不是故意虐待你的。请注意,
span
只是文本节点的“容器”。单击
按钮
节点应足以触发所需的行为它不会滥用。。。这是重要的反馈,我同意他的观点。您的定位器通常非常糟糕。当您有ID时,没有理由拥有所有这些属性和类。当您有@Andersson已经提到的
按钮时,没有理由单击
SPAN
。这不会给Andrei的答案增加任何有价值的内容,如果您想提交
表单
,您可以尝试
驱动程序。通过id(“bookButton”)查找元素。提交()
,而不是单击。还要注意的是,有不止一个按钮的类名为“btn primary”
,因此实际上您处理的元素是错误的Hi Andrej感谢您的解决方案,但在等待之后,我收到了另一个错误,即。TimeoutException:消息:知道如何解决此问题吗?Hi@Kyu.ki,检查按钮是否位于
iframe/frame
的内部。你也可以试试JSExecutor。我现在将添加到示例代码的答案中。不,它不在iframe下,但在main class标记中,请参考这个URL“”,我现在用javascript executor尝试过,它对我有效。您也可以尝试一下(在我的回答中,我提供了
exectute\u脚本
的示例代码)。PS:如果没有javascript执行器,似乎不可能单击按钮。