异常org.openqa.selenium.ElementNotVisibleException:..元素不可见

时间:2018-02-16 07:08:34

标签: java selenium selenium-webdriver

尝试选择单选按钮但获取" 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);

    }

}

3 个答案:

答案 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();