gpt4 book ai didi

java - 我收到 NoSuchElement 异常错误

转载 作者:行者123 更新时间:2023-11-28 20:22:24 24 4
gpt4 key购买 nike

我已经分享了下面的代码,请让我知道更正

import org.openqa.selenium.By;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
/*System.setProperty("webdriver.ie.driver","D:\\Backup\\Documents\\Automation\\drivers\\IEDriverServer.exe");
InternetExplorerDriver driver = new InternetExplorerDriver();
driver.get("https://www.google.com");

WebDriverWait driverWait = new WebDriverWait(driver,50);

driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lst-ib']"))).click();
driver.findElementByXPath("//*[@id='lst-ib']").sendKeys("Make My Trip");
driver.findElementById("_fZl").click();
driverWait.until(ExpectedConditions.elementToBeClickable(By.linkText("MakeMyTrip, India's No 1 Travel Site | Book Hotels, Flights, Holiday ..."))).click();
Screenshot S1 = new Screenshot();
S1.Takescreen();*/
String username = null;
String password = null;
MyClass C1 = new MyClass();
C1.URLs(username, password);
}

public void URLs (String username, String password) throws InterruptedException
{

System.setProperty("webdriver.ie.driver","D:\\Backup\\Documents\\Automation\\drivers\\IEDriverServer.exe");
InternetExplorerDriver driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
driver.findElement(By.className("lst lst-tbb sbibps")).sendKeys("irctc");
driver.findElement(By.id("_fZl")).click();
Thread.sleep(10000);
driver.findElement(By.linkText("IRCTC Next Generation eTicketing System")).click();
username = driver.findElement(By.id("usernameId")).getTagName();
password = driver.findElement(By.className("loginPassword")).getTagName();
System.out.println(username);
System.out.println(password);
}
}

错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with class name == lst lst-tbb sbibps (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 10.45 seconds

最佳答案

正如@Guy 所指出的,您尝试访问 3 个不同的类,因此不能对所有类使用 By.className 方法。

我认为 Guy 的方法是正确的,但只有当您的用例与 Guy 理解的完全一样时它才有效

您的用例是什么?

  1. 你想访问所有或者lst的元素吗? , lst-tbbsbibps或者
  2. 您想访问所有 3 类的所有元素吗lst , lst-tbbsbibps或者
  3. 你想访问类sbibps的所有元素吗? ,它是 lst-tbb 的子类,它是 lst 的子类(即 .lst.lst-tbb.sbibps )

解决方案

  1. 对于第一个用例,这应该足够了(参见 Selenium Webdriver w/Java: locating elements with multiple class names with one command)

driver.findElements(By.cssSelector(".lst,.lst-tbb,.sbibps");

  1. 对于第二个用例,我发现了这个(参见 Find div element by multiple class names?)

driver.findElements(By.xpath("//*[@class='lst lst-tbb sbibps]"));

或者这个,如果你不知道它是否有更多的类

driver.findElements(By.xpath("//*[contains(@class, 'lst lst-tbb sbibps')]"));

  1. 对于最后一个用例,这应该有效(归功于@Guy)

driver.findElements(By.cssSelector(".lst.lst-tbb.sbibps"));

注意!我使用了findElements方法而不是 findElement方法,它会产生一个 List 并且不会抛出异常,而是如果不满足条件,则列表只是空的。

还有:见Need to find element in selenium by css供引用

关于java - 我收到 NoSuchElement 异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com