无法单击selenium中的列表按钮选项:异常元素不可见

时间:2015-06-13 11:19:51

标签: selenium

您好我正在尝试在列表选项冷和流感上选择但我得到的元素不可见异常,有人可以帮我如何在selenium脚本中选择它。

网站网址:http://www.healthdirect.gov.au/symptom-checker/tool?symptom=COLD

感冒和流感

这是我的硒代码

public void selectspecificSymptom(String arg) throws InterruptedException {
    // TODO Auto-generated method stub
    switch(arg.toLowerCase())
    {
    case "cold and flu":

    /*  Actions actions = new Actions(driver);
        WebElement menuHoverLink = driver.findElement(By.xpath("//*[@ng-true-value='49|Feeling sick or unwell|1']"));
        actions.moveToElement(menuHoverLink);
        actions.click();
        actions.perform();*/

        Thread.sleep(5000);

        driver.findElement(By.xpath("//*[@ng-true-value='49|Feeling sick or unwell|1']")).click();

        driver.findElement(By.id("submitAnswer"));
        break;
    default:
        System.out.println("Not a valid option");
        break;
    }   


}

1 个答案:

答案 0 :(得分:0)

试用此代码:

public class Stack{

    @Test public static void select() throws InterruptedException{

            WebDriver driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
            driver.get("http://www.healthdirect.gov.au/symptom-checker/tool");

            //wait for the iframe and switch

            WebDriverWait wait = new WebDriverWait(driver, 30);
            WebElement frame = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@id, 'easyXDM_default')]")));
            driver.switchTo().frame(frame);
            Thread.sleep(11000);

            //Get all your health topics and select one which matches

            List<WebElement> l = driver.findElements(By.xpath("//*[@id='leftCol']/div/button"));

            for (int i = 0; i < l.size(); i++) {
                System.out.println(l.get(i).getText());
                    if (l.get(i).getText().equalsIgnoreCase("Ear, nose and throat")){
                        l.get(i).click();
                        Thread.sleep(2000);

                        //Get all your symptoms and select the needed

                        List<WebElement> l1 = driver.findElements(By.xpath("//*[@id='contentHeight']/div[2]/button"));
                        //Place your second for loop for the second list here
                        l1.get(1).click(); //just to test your 'Colds and flu' link

                    }
            }

备注:

  • 那里有一个iframe,所以你需要在点击你的元素之前切换到那个
  • 我在尝试时网络非常糟糕,所以调整你的thread.sleeep()或等待明确等待的元素
  • 如果您想在外面操作
  • ,请记得从iframe切换回来