Java 获取错误NoTouchElementException:无法在“中”找到元素;big.findElement(By.xpath(";//*[@id=';rso';]/div[1]/div/div/h3/a";)。单击()&引用;
代码在位置失败Java 获取错误NoTouchElementException:无法在“中”找到元素;big.findElement(By.xpath(";//*[@id=';rso';]/div[1]/div/div/h3/a";)。单击()&引用;,java,eclipse,selenium-webdriver,Java,Eclipse,Selenium Webdriver,代码在位置失败 package FlipPkg; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class BigBasket { public static void main(String[] args) {
package FlipPkg;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BigBasket {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users\\NP031997\\Downloads\\geckodriver-v0.12.0-win64\\geckodriver.exe");
WebDriver big = new FirefoxDriver();
big.get("https://www.google.com/");
big.manage().window().maximize();
System.out.println("The webpage is:" + big.getTitle());
big.findElement(By.xpath(".//*[@id='gs_htif0']")).sendKeys("Big Basket");
big.findElement(By.xpath(".//*[@id='gs_htif0']")).sendKeys(Keys.ENTER);
big.findElement(By.xpath("//*[@id='rso']/div[1]/div/div/h3/a")).click();
System.out.println("The current webpage is:" + big.getTitle());
}
}
您的代码很好。WebDriver肯定会找到该元素,但您在该代码中遇到了一个小问题。无接触异常可能有两个原因:
big.findElement(By.xpath("//*[@id='rso']/div[1]/div/div/h3/a")).click();
WebDriverWait wait = new WebDriverWait(big,120);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='rso']/div[1]/div/div/h3/a"));
WebDriverWait wait = new WebDriverWait(big, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Big Basket")));
通过使用第2和第3种方法,您将获得一个元素列表,因为许多元素都包含文本大篮子。您的代码很好。WebDriver肯定会找到该元素,但您在该代码中遇到了一个小问题。无接触异常可能有两个原因:
big.findElement(By.xpath("//*[@id='rso']/div[1]/div/div/h3/a")).click();
WebDriverWait wait = new WebDriverWait(big,120);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='rso']/div[1]/div/div/h3/a"));
WebDriverWait wait = new WebDriverWait(big, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Big Basket")));
通过使用第二和第三种方法,您将得到一个元素列表,因为许多元素都包含文本大篮子。感谢您的输入。问题解决了。我使用的代码是:WebDriverWait wait=newwebdriverwait(大,15);等待.until(ExpectedConditions.elementToBeClickable(By.partialLinkText(“大篮子”)))。单击();正如你所说,你的问题已经解决了。然后请将答案标记为正确,以便其他人不会浪费时间再次检查问题并尝试给出答案。谢谢您的输入。问题解决了。我使用的代码是:WebDriverWait wait=newwebdriverwait(大,15);等待.until(ExpectedConditions.elementToBeClickable(By.partialLinkText(“大篮子”)))。单击();正如你所说,你的问题已经解决了。然后请将答案标记为正确,这样其他人就不会浪费时间再次检查问题并尝试给出答案。