我想使用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)
有人可以帮我解决这个问题吗。
答案 0 :(得分:1)
根据抛出的异常,您正在等待超时运行。
在默认超时20秒或您使用的xPath错误后,您的对象不存在。您可以尝试使用以下xPath选择第二个标签:(//input[@name="request_for"])[2]
Tipp:为了快速测试,将HTML代码传递到xPath生成器(例如:https://www.freeformatter.com/xpath-tester.html)并测试xPath表达式,而无需重新运行Selenium。
答案 1 :(得分:0)
根据 HTML ,您已共享并且您的代码试验将继续进行,因为您将单击其中一个单选按钮而不是 ExpectedConditions visibilityOfElementLocated()
您需要使用 elementToBeClickable()
,如下所示:
要点击与文字自我关联的单选按钮,您可以使用以下代码行:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ng-pristine ng-untouched ng-valid' and @value='self']"))).click();
要点击与文字其他关联的单选按钮,您可以使用以下代码行:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ng-pristine ng-untouched ng-valid' and @value='other']"))).click();
答案 2 :(得分:0)
尝试使用visibilityOfElementLocated()
代替{{1}}