简体   繁体   中英

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

<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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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