java - 无法使用 selenium java 选择单选按钮

标签 java selenium selenium-webdriver xpath selenium-chromedriver

我想使用 Selenium Web 驱动程序选择一个单选按钮。我的代码是

<div class="radio-switch-group" aria-label="RequestForCheckBox" role="menu">
<label>
    <input type="radio" name="request_for" value="self" ng-model="data.form.requestFor" class="ng-pristine ng-untouched ng-valid">
    <span class="radio-label" translate=""><span class="ng-scope">Self</span></span>
</label>
<label>
    <input type="radio" name="request_for" value="other" data-ng-click="$scope.getDataHandler()" ng-model="data.form.requestFor" class="ng-pristine ng-untouched ng-valid">
    <span class="radio-label" translate=""><span class="ng-scope">Others</span></span>
</label>
</div>

我尝试使用下面所示的代码来定位元素

WebElement other = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"make-request-form\"]/div/section/div[2]/div/form/div[1]/div/div/div/label[2]/input")));
other.click();

但是低于

 Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //*[@id="make-request-form"]/div/section/div[2]/div/form/div[1]/div/div/div/label[2]/input (tried for 20 second(s) with 500 milliseconds interval)
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:08.638Z'
System info: host: 'AUUR01VP1341', ip: '10.97.2.56', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptSslCerts: true, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.34.522940 (1a76f96f66e3ca..., userDataDir: C:\Users\L100455\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 61.0.3163.100, webStorageEnabled: true}
Session ID: 9d15042c1c2c4eda197796e5fdf42243
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
    at auto.Automation.main(Automation.java:44)

有人可以帮我解决这个问题吗?

最佳答案

根据抛出的异常,您正在等待超时中运行。

在默认的 20 秒超时后,您的对象不存在,或者您使用的 xPath 错误。您可以尝试使用以下xPath来选择第二个标签:(//input[@name="request_for"])[2]

提示:为了进行快速测试,请将 HTML 代码传递到 xPath 生成器(例如: https://www.freeformatter.com/xpath-tester.html )并测试 xPath 表达式,而无需重新运行 Selenium。

关于java - 无法使用 selenium java 选择单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50253682/

相关文章:

selenium - 如何等到 Selenium WebDriver 中的页面卡住?

java - 从 PlayFramework 表单发布 JSON 数据

java - 如何知道网站支持的语言?

Python Selenium add_extensions 关闭安装启动画面

c# - Selenium WebDriver IE 性能

python - "While True Loop"不会导致函数再次执行

ios - Python 3 错误 : ImportError: No module named selenium

java - 如何在 iText 生成的 PDF 中向我的页眉添加图像?

java - 如何为多个组件创建一个单一的 actionlistener 函数

java - android中简单的递增/递减程序