有没有办法在selenium中访问禁用的文本框?

时间:2015-09-20 13:37:47

标签: selenium search selenium-webdriver textbox webdriver

最近我看到Google改变了主页并使搜索框变得可扩展,因此我无法使用以便在搜索框中提供文字:

driver.findElement(By.id("gs_htif0")).sendKeys("Toi");

注意:搜索框的标记值:disabled =""

PFB Selenium代码:

public class Navigation {

    public static WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\resources\\"+"chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://www.google.com");
        Thread.sleep(4000);
        if (driver.findElement(By.id("gs_htif0")).isEnabled()){
            System.out.println("This is siddhant");
            driver.findElement(By.id("gs_htif0")).sendKeys("Toi");
            /*driver.navigate().back();         
            driver.navigate().forward();*/

        }
        else {
            WebElement textbox = driver.findElement(By.id("gs_htif0"));
            ((JavascriptExecutor) driver).executeScript("arguments[0].enabled = true", textbox); 
            //driver.findElement(By.xpath(".//*[@id='gs_htif0']")).sendKeys("Hello");
            Thread.sleep(4000);
            textbox.sendKeys("hello");          
        }       
        driver.quit();
    }

}

1 个答案:

答案 0 :(得分:0)

您真正要做的是删除disabled块中的else属性

((JavascriptExecutor) driver).executeScript("$('#gs_htif0').removeAttribute('disabled')"); 
textbox.sendKeys("hello");