尝试选择单选按钮但获取" ElementNotVisibleException:" exception.please参考下面的代码
public class Automatecheckbox {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "G:/chromedriver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
WebElement male_radio_button=driver.findElement(By.xpath(".//*[@id='u_0_c']"));
boolean status=male_radio_button.isDisplayed();
System.out.println("Male radio button is Displayed >>"+status);
boolean enabled_status=male_radio_button.isEnabled();
System.out.println("Male radio button is Enabled >>"+enabled_status);
boolean selected_status=male_radio_button.isSelected();
System.out.println("Male radio button is Selected >>"+selected_status);
Thread.sleep(1000);
male_radio_button.click();
boolean selected_status_new=male_radio_button.isSelected();
System.out.println("Male radio button is Selected >>"+selected_status_new);
}
}
答案 0 :(得分:0)
尝试使用此代码查找元素:
WebElement male_radio_button = driver.findElement(By.xpath("//input[@id='u_0_c']"));
希望它能起作用!!
答案 1 :(得分:0)
使用显式等待(WebDriverWait)让WebElement在执行任何执行之前加载。 WebDriverWait对象(在本例中为wait
)在调用ChromeDriver();
后立即初始化
WebDriver driver = new ChromeDriver();
/* Initialize the WebDriverWait, with 30 seconds of wait time. */
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='u_0_c']")));
WebElement male_radio_button=driver.findElement(By.xpath(".//*[@id='u_0_c']"));
答案 2 :(得分:0)
使用以下代码:
driver.findElement(By.xpath("//input[@value='2']")).click();