简体   繁体   English

如何通过python和selenium在html上单击文本为Continue的按钮

[英]How to click on the button with text as Continue as per the html through python and selenium

I am trying to click on the following button "Continue" using Selenium in Python. 我试图在Python中使用Selenium单击以下按钮“继续”。

<div class="sb-modal-section">
    <button class="sb-modal-button primary">Continue</button>
    <button class="sb-modal-button">Rules</button>
</div>

I am able to locate the button using many methods including: 我可以使用许多方法来找到按钮,包括:

x = driver.find_element_by_css_selector('.sb-modal-button.primary')

or 要么

x = driver.find_element_by_xpath("//button[@class='sb-modal-button primary']")

which I know isn't supposed to work because of the space. 我知道这不应该因为空间而起作用。

However, 然而,

x.click()

doesn't perform the continue action. 不执行继续动作。 I have tried implicit and explicit waits, but that doesn't seem to be the problem. 我尝试了隐式和显式等待,但这似乎不是问题。 It finds them both and can display x.text. 它同时找到它们和可以显示x.text。 But the click doesn't work. 但是点击不起作用。 Any help is appreciated. 任何帮助表示赞赏。

您可以使用此:

driver.execute_script("arguments[0].click();", x) 

As per the HTML you have provided it seems the element with text as Continue is within a Modal Dialog Box so you have to induce WebDriverwait for the element to be clickable as follows: 根据您提供的HTML,似乎带有继续文本的元素位于模态对话框中,因此您必须诱使WebDriverwait使元素可单击 ,如下所示:

  • CSS_SELECTOR : CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.sb-modal-section>button.sb-modal-button.primary"))).click() 
  • XPATH : XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='sb-modal-section']//button[@class='sb-modal-button primary'][contains(.,'Continue')]"))).click() 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Selenium和Python根据html单击带有文本的按钮作为搜索? - How to click on the button with text as Search as per the html through Selenium and Python? 如何通过Selenium和Python按照HTML来点击引导按钮 - How to click on a bootstrap button as per the HTML through Selenium and Python 如何通过Selenium和Python按html单击元素 - How to click on the element as per the html through Selenium and Python 如何通过 Selenium Python 根据 HTML 在复选框内单击 - How to click within the checkbox as per the HTML through Selenium Python 如何使用Selenium和Python按HTML单击单选按钮 - How to click on a Radio Button as per the HTML using Selenium and Python 如何使用 Selenium 和 Python 根据 html 定位带有文本作为注释的按钮? - How to locate the button with text as Comment as per the html using Selenium and Python? 如何使用 python 中的 selenium 在网站中单击继续按钮? - How to click the Continue button within a website using selenium in python? 如何使用 python 中的 selenium 在网站上单击继续按钮? - How to click the Continue button on website using selenium in python? 如何通过selenium-webdriver和Python根据html在没有唯一标识符的文本框中单击 - How to click within the textbox with no unique identifier as per the html through selenium-webdriver and Python 如何通过Selenium和Python提供的HTML单击基于角度的复选框? - How to click on the angular based checkbox as per the HTML provided through Selenium and Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM