英文:
Test lufthansa, element is not found although XPATH exists and was copied from the inspector tools by right clicking on the page and accessing HTML
问题
我正在尝试编写一个Selenium脚本来测试汉莎航空的网站。我想将"RBA"作为出发机场,"JFK"作为目的机场输入。然而,尽管我在检查工具中右键单击元素并复制XPATH,但我仍然收到以下错误消息:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"];}
(Session info: chrome=114.0.5735.110)
这是我使用的代码:
package Test;
// 由Selenium IDE生成
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
public class AirLineTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void test() {
driver.get("https://www.lufthansa.com/ma/en/homepage");
driver.manage().window().setSize(new Dimension(1552, 840));
driver.findElement(By.xpath("//*[@id=\"cm-acceptAll\"]")).click();
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"]"))
.sendKeys("RBA");
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].destinationCode\"]"))
.sendKeys("JFK");
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].travelDatetime\"]"))
.click();
driver.close();
}
}
希望这能帮助你解决问题。
英文:
I am attempting to write a Selenium script to test the Lufthansa website. I want to enter "RBA" as the origin airport and "JFK" as the destination airport. However, I am receiving the following error message despite copying the XPATH by right-clicking on the element in the inspector tools:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode"]"}
(Session info: chrome=114.0.5735.110)
This is the code I am using:
package Test;
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
public class AirLineTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void test() {
driver.get("https://www.lufthansa.com/ma/en/homepage");
driver.manage().window().setSize(new Dimension(1552, 840));
driver.findElement(By.xpath("//*[@id=\"cm-acceptAll\"]")).click();
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"]"))
.sendKeys("RBA");
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].destinationCode\"]"))
.sendKeys("JFK");
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].travelDatetime\"]"))
.click();
driver.close();
}
}
答案1
得分: 1
根本原因: 此XPath表达式//*[@id="dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode"]
,您用于定位元素的ID
属性是不正确的,因为ID
的值是动态的。每次页面加载时,值都不同。因此,请将您的XPath表达式更改如下所示:
将这些行更改为:
driver.findElement(By.xpath("//input[@placeholder='From']")).clear();
driver.findElement(By.xpath("//input[@placeholder='To']")).sendKeys("JFK");
英文:
Root Cause: This XPath expression //*[@id="dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode"]
which you are using to locate the element's ID
attribute is incorrect, because value of ID
is dynamic. Each time the page loads the value is different. Hence change your XPath expression as shown below:
Change these lines:
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].destinationCode\"]"))
.sendKeys("JFK");
To:
driver.findElement(By.xpath("//input[@placeholder='From']")).clear();
driver.findElement(By.xpath("//input[@placeholder='To']")).sendKeys("JFK");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论