python - 如何在 Selenium 中找到这些复选框元素

标签 python selenium select element

我找不到复选框 on this site Selenium

我尝试过:Xpath、ID、类型、操作键、文本和部分文本。我已经能够发送 key (用户和密码)并找到用户/通行证元素。

  • 代码:

  • 标签=“acceptTermsAndConditions”class=“checkbox-label-margin”> -#我接受条款和条件

  • xpath -//*[@id="loginForm"]/div/div[3]/label 1

感谢您提前提供的帮助,我感觉自己在兜圈子。

最佳答案

TL;DR 请参阅下面带有澄清注释的代码。

它对您不起作用的原因之一可能是您需要等待表单渲染、可见和可交互。这可以通过 Explicit Wait 来解决.

通过 .click() 单击“接受条款”时我注意到的另一个问题是,由于单击发生在元素中间,因此会打开使用条款在单独的选项卡中,这是不可取的。您可以通过点击偏移(0, 0) 来解决此问题 Action Chains .

就使用 Selenium 定位器访问复选框而言,可以通过多种不同的方式来完成。在下面的代码中,我在检查 label 元素的 for 属性值时使用了 CSS 选择器

工作代码:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()
driver.get('https://onecall.1100.com.au/au-b4-en/Account/Login')

wait = WebDriverWait(driver, 10)

# wait for the form to get visible
login_form = wait.until(EC.visibility_of_element_located((By.ID, 'loginForm')))

# accept terms
accept_terms = login_form.find_element_by_css_selector('label[for=acceptTermsAndConditions]')
ActionChains(driver).move_to_element_with_offset(accept_terms, 0, 0).click().perform()

# keep me logged in
login_form.find_element_by_css_selector('label[for=checkbox2]').click()

# take a screenshot to prove it is working
login_form.screenshot('form.png')

而且,这就是您将在 form.png 中看到的内容:

enter image description here

关于python - 如何在 Selenium 中找到这些复选框元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58533322/

相关文章:

mysql - 将 AES_DECRYPT 与 SELECT * 一起使用

python - Spark MLLIB LDA 主题矩阵的输出是什么?

python - 如何消除该函数中的一个参数?

selenium - Capybara switch_to.alert(对话框)间歇性故障

RSelenium:配置 Firefox Remotedriver 以使用 Tor 网络

select - 如何在 HIVE 的 CASE 语句中使用 SET 列表变量

MySQL 从具有不同列号的多个表中选择

python - 理解 numpy.gradient

python pandas.Series.isin 不区分大小写

java - 在 Java 中使用 Selenium 单击动态下拉 div