本文介绍了如何在网站https://www.phptravels.net上通过Selenium和Java调用显式等待以文本为元素的元素,单击“我的帐户"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写以下代码,以单击文本为我的帐户的元素.它显示元素不可见".为了解决这个问题,我尝试等待,但是超时.有没有办法.您可以在下面找到我的代码:

I am writing the following code to click on the element with text as My Account. It is showing "element not visible". To resolve the issue I am trying expected wait, but it is being timed out. Is there any way around. You can find my code below:

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

推荐答案

对于文本为我的帐户的元素上的click(),而不是visibilityOfElementLocated(),您需要使用 WebDriverWait 通过 elementToBeClickable() 方法,您可以使用以下解决方案:

To click() on the element with text as My Account instead of visibilityOfElementLocated() you need to induce WebDriverWait through elementToBeClickable() method and you can use the following solution:

  • 代码块:

  • Code Block:

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

  • 浏览器快照:

  • Browser Snapshot:

    这篇关于如何在网站https://www.phptravels.net上通过Selenium和Java调用显式等待以文本为元素的元素,单击“我的帐户"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 11-01 15:08