简体   繁体   English

在视野之外使用 selenium webdriver java 选择单选按钮

[英]Selecting a radio button using selenium webdriver java when out of view

When I am running the below code run, it only works if I actually manage to scroll to the radio button to get it on the screen in time, otherwise the radio button is not selected.当我运行下面的代码运行时,它只有在我确实设法滚动到单选按钮以及时将其显示在屏幕上时才有效,否则未选择单选按钮。

HTML HTML

<label><input name="GenericID1" type="radio" value="5625">&nbsp;Sample;|&nbsp;Sat/15/805B</label>

WebDriver网络驱动程序

WebDriver driver = new FirefoxDriver();
driver.get("http://samplewebste.com");
WebElement oCheckBoxTest = driver.findElement(By.cssSelector("input[value='5625']"));
oCheckBoxTest.click();

Does anyone have any idea why I actually have to manually scroll to the radio to get it to select actually work, otherwise the command just seems to be ignored without throwing any exceptions?有谁知道为什么我实际上必须手动滚动到收音机才能让它选择实际工作,否则该命令似乎被忽略而不会引发任何异常?

Try following code to scroll to required element and click on it:尝试使用以下代码滚动到所需元素并单击它:

WebElement oCheckBoxTest = driver.findElement(By.cssSelector("input[value='5625']"));
Actions actions = new Actions(driver);
actions.moveToElement(oCheckBoxTest);
actions.click();
actions.perform();

If it not works, try with JavaScript :如果它不起作用,请尝试使用JavaScript

WebElement oCheckBoxTest = driver.findElement(By.cssSelector("input[value='5625']"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", oCheckBoxTest);
oCheckBoxTest.click()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM