isEnabled()无法找到元素

时间:2017-02-23 14:10:13

标签: selenium isenabled

我是硒的新手。我试图通过isEnabled()检查按钮是否启用。但是当我运行这个程序时,它会产生一个错误,如按钮的“无法定位元素”。

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class test 
{
static WebDriver driver;
public static void main(String[] args) 
{
    System.setProperty("webdriver.chrome.driver", "D:\\rakesh\\software\\selenium browser\\New folder\\chromedriver.exe");
    driver=new ChromeDriver();
    driver.get("https://app.crossover.com/");
    driver.manage().window().maximize();
    JavascriptExecutor js= (JavascriptExecutor) driver;
    js.executeScript("window.scrollBy(0,5500)", "");

    driver.findElement(By.linkText("Available Jobs")).click();

    Boolean search_btn_ele = driver.findElement(By.xpath(".//*[@id='available-jobs']/div[2]/form/div/div[3]/button")).isEnabled();

    if(search_btn_ele.FALSE)
    {
        System.out.println("Button is disable before giving search keys");
    }
    else
    {
        System.out.println("Button is enable before giving search keys");
    }

    WebElement search_txtfield_ele= driver.findElement(By.xpath(".//*[@id='available-jobs']/div[2]/form/div/div[1]/div/input"));
    search_txtfield_ele.sendKeys("Chief");
}
}

1 个答案:

答案 0 :(得分:1)

使用WebDriverWait等待元素存在:

new WebDriverWait(driver, TimeSpan.FromSeconds(45)).Until(ExpectedConditions.ElementIsVisible((By.Id("ctl00_ContentPlaceHolder1_drp85"))));