0

我正在编写以下代码以单击文本为My Account的元素。它显示“元素不可见”。为了解决这个问题,我正在尝试预期的等待,但它正在超时。有什么办法。你可以在下面找到我的代码:

package com.php.travels;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LogIn {

public static void main(String[] args) {
    System.setProperty(
      "webdriver.chrome.driver", 
      "/Users/owner/desktop/chromedriver");

    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.get("https://www.phptravels.net");
    try {
       WebDriverWait wait=new WebDriverWait(driver, 20);

       wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.xpath("//li[@id ='li_myaccount']/a"))
       ).click();

    }
    catch(Throwable t){
        System.out.println("The execption is: " + t);
    }
    finally {
        System.out.println("If no exception tell me now");
    }
}

} // end class
4

1 回答 1

0

click()使用文本作为我的帐户而不是visibilityOfElementLocated()您需要通过方法诱导WebDriverWaitelementToBeClickable()的元素,您可以使用以下解决方案:

  • 代码块:

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.get("http://www.phptravels.net");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//nav[@class='navbar navbar-default']//li[@id='li_myaccount']/a[normalize-space()='My Account']"))).click();
    
  • 浏览器快照:

php_travels_login_click

于 2018-07-11T16:22:35.367 回答