[英]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.