java
  • selenium
  • selenium-webdriver
  • 2016-04-22 177 views 0 likes 
    0

    我一直在玩java的硒,我遇到了一個問題,我不知道。 http://www.legalcontracts.com/contracts/lease-agreement-form/?ldcn=menu+nameorg.openqa.selenium.ElementNotVisibleException點擊繼續按鈕

    從第三頁(地址頁)要到第四 我能夠設置地址:

    這個頁面在這裏

    @FindBy(xpath="//div[@class=' qd spropertyAddress']/div[@class='ans']/input") 
        private WebElement address; 
    
    public void SetAddress(String addressName){ 
         address.sendKeys(addressName); 
        } 
    

    但是當我試圖繼續

    @FindBy(xpath="//a[contains(text(),'Continue')]") 
        private WebElement continuebutton; 
    continuebutton.click(); 
    

    我收到一個org.openqa.selenium.ElementNotVisibleException。繼續按鈕與前一頁的編號相同,因此我不確定這是否與它有關。我嘗試了很多不同的方法來嘗試處理這個問題,但似乎可行的唯一方法是thread.sleep,這不是一個很好的處理方法。

    東西我曾嘗試:

    WebDriverWait wait = new WebDriverWait(driver,30); 
         wait.until(ExpectedConditions.elementToBeClickable(continuebutton)); 
         wait.until(ExpectedConditions.elementToBeClickable(continuebutton)); 
         wait.until(ExpectedConditions.presenceOfElementLocated((By) continuebutton)); 
         wait.until(ExpectedConditions.visibilityOfElementLocated((By) continuebutton)); 
    
    
    public void waitForJQuery(WebDriver driver) { 
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { 
         public Boolean apply(WebDriver d) { 
          JavascriptExecutor js = (JavascriptExecutor) d; 
          return (Boolean) js.executeScript("return !!window.jQuery && window.jQuery.active == 0"); 
         } 
        }); 
    } 
    
    public void waitForJavaScript(WebDriver driver){ 
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { 
         public Boolean apply(WebDriver driver) { 
          return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"); 
         } 
        } 
    ); 
    
    public void checkPageIsReady(WebDriver driver) { JavascriptExecutor js = (JavascriptExecutor)driver; //Initially bellow given if condition will check ready state of page. 
    
        if (js.executeScript("return document.readyState").toString().equals("complete")){ 
         System.out.println("Page Is loaded."); 
         return; 
         } 
    
        //This loop will rotate for 25 times to check If page Is ready after every 1 second. 
        //You can replace your value with 25 If you wants to Increase or decrease wait time. 
        for (int i=0; i<25; i++){ 
         try { 
          Thread.sleep(1000); } 
         catch (InterruptedException e) {} 
    
         //To check page ready state. 
         if (js.executeScript("return document.readyState").toString().equals("complete")){ 
          break; } } 
        } 
    

    我進來從以前的頁面相同的繼續進行,但在不同的頁面對象:

    @FindBy(xpath="//a[contains(text(),'Continue')]") 
        private WebElement continuebutton; 
    continuebutton.click(); 
    

    能硒是混淆了當前繼續按鈕,前一個?

    回答

    0

    使用下面的代碼:

    driver.findElement(By.cssSelector("div.ans>input#ctl00_Content_ctrlQuestions_ctl194_ctl21_ctl06_ctl01_ctl01_ctl01_in")).click(); 
    
    driver.findElement(By.cssSelector("div.ans>input#ctl00_Content_ctrlQuestions_ctl194_ctl21_ctl06_ctl01_ctl01_ctl01_in")).clear(); 
    
    driver.findElement(By.cssSelector("div.ans>input#ctl00_Content_ctrlQuestions_ctl194_ctl21_ctl06_ctl01_ctl01_ctl01_in")).sendKeys("ur address"); 
    
    Thread.sleep(2000); 
    
    //click on continue button 
    
    driver.findElement(By.cssSelector(" .largeProceedButton.continueButtonnContinueFirst.nContinue")).click(); 
    

    ,而不是使用XPath的就是我上面提到點擊繼續按鈕cssSelector。

    +0

    @eradicator,這工作? – noor

    +0

    我想在我的代碼中不使用任何thread.sleeps。如果我使用睡眠一切正常,但它不保證點擊將起作用 – eradicator

    +0

    我只是使用它來進行測試......無需使用此等待.....您可以單擊繼續按鈕而無需這等待....你告訴你,我不能點擊繼續按鈕...只需使用我的css路徑,點擊 – noor

    相關問題