我已经分享了以下代码,请让我知道更正
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);
}
}
错误:
线程中的异常" main" org.openqa.selenium.NoSuchElementException: 无法找到类名== lst lst-tbb sbibps的元素(警告: 服务器没有提供任何堆栈跟踪信息)命令 持续时间或超时:10.45秒
答案 0 :(得分:2)
正如@Guy指出的那样,你试图访问3个不同的类,因此不能对所有类使用By.className方法。
我认为盖伊有正确的方法,但只有当你的用例完全像盖伊所理解的那样它才有效
lst
,lst-tbb
或sbibps
或lst
,lst-tbb
或sbibps
或sbibps
的所有元素,它是lst-tbb
的子类,lst
的子类(即.lst.lst-tbb.sbibps
)< / LI>
醇>
driver.findElements(By.cssSelector(".lst,.lst-tbb,.sbibps");
driver.findElements(By.xpath("//*[@class='lst lst-tbb sbibps]"));
或者,如果你不知道它是否有更多的课程
driver.findElements(By.xpath("//*[contains(@class, 'lst lst-tbb sbibps')]"));
driver.findElements(By.cssSelector(".lst.lst-tbb.sbibps"));
注意!我使用的是findElements
方法,而不是findElement
方法,它会产生一个List并且不会抛出异常,而是列表是如果不满足条件,则为空。
此外: 请参阅Need to find element in selenium by css以供参考
答案 1 :(得分:1)
lst lst-tbb sbibps
实际上是3个不同的类。要使用所有这些,您可以使用cssSelector
driver.findElement(By.cssSelector(".lst.lst-tbb.sbibps")).sendKeys("irctc");
使用className
,您只能使用一个类,但它们看起来都不是唯一的
driver.findElement(By.className("lst")).sendKeys("irctc");
// or
driver.findElement(By.className("lst-tbb")).sendKeys("irctc");
// or
river.findElement(By.className("sbibps")).sendKeys("irctc");
答案 2 :(得分:0)
在Google.com中,通常有三个输入标签可供搜索,但其中有两个属性为hidden = true
<div id="gs_sc0" class="gsfi" style="background: transparent none repeat scroll 0% 0%; color: transparent; padding: 0px; position: absolute; z-index: 2; white-space: pre; visibility: hidden;"/>
<input id="gs_taif0" class="gsfi" disabled="" autocomplete="off" aria-**hidden="true"** style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; left: 0px; visibility: hidden;" dir="ltr"/>
<input id="gs_htif0" class="gsfi" disabled="" autocomplete="off" aria-hidden="true" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; transition: all 0.218s ease 0s; opacity: 0; text-align: left; left: 0px;" dir="ltr"/>
所以我们必须使用未隐藏的输入,在这种情况下使用
driver.findElement(By.id("lst-ib")).sendKeys("irctc");
答案 3 :(得分:0)
请尝试以下代码。
driver.get("http://google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.id("lst-ib")).sendKeys("irctc");
WebDriverWait wait = new WebDriverWait(driver, 5); //use explicit wait method for 5 seconds until element not found.
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//a[text()='IRCTC Next Generation eTicketing System']"))));
driver.findElement(By.xpath("//a[text()='IRCTC Next Generation eTicketing System']")).click();
WebDriverWait homepage = new WebDriverWait(driver, 5); //use explicit wait method for 5 seconds until element not found. Because when click on link button page is getting loading so wait for few seconds.
homepage.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("usernameId"))));
driver.findElement(By.id("usernameId")).sendKeys("Username");
driver.findElement(By.xpath("//input[@class='loginPassword'][@type='password']")).sendKeys("XXXXX");
注意:而不是Thread.sleep()
,请使用Explicit wait
方法。
答案 4 :(得分:0)
使用以下内容:
driver.findElement(By.name("q")).sendKeys("irctc");
答案 5 :(得分:0)
请使用以下代码,希望它有效。
driver.findElement(By.className("gsfi")).sendKeys("irctc");
答案 6 :(得分:0)
尝试以下代码,它将适用于您的情况(我在我的机器上测试过)。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MyClass {
public static void main(String[] args) throws InterruptedException {
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();
InternetExplorerDriver driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
driver.findElement(By.id("lst-ib")).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);
}
}
让我知道它是否适合你。