2015-09-20 23 views
1

最近我看到,谷歌改變了它的主頁和做搜索框diasable,所以我不能夠使用序在搜索框提供文本:有什麼方法可以訪問硒中禁用的文本框?

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

注:搜索框的標誌值:disabled =""

PFB硒代碼:

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(); 
    } 

} 
+0

我不確定你在問什麼。 google.com網頁上沒有包含gs_htif0 ID的元素。但是...搜索框顯然存在,而不是禁用,否則沒有人能夠進行搜索。搜索框的ID可以在您喜歡的瀏覽器中使用devtools找到。請澄清你的問題。 – JeffC

回答

0

你真正想要做的是去除disabled屬性在else

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