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

huangapple go评论65阅读模式
英文:

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: {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;//*[@id=&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode&quot;]&quot;}
(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&lt;String, Object&gt; vars;
    JavascriptExecutor js;

    @Before
    public void setUp() {
        driver = new ChromeDriver();
        js = (JavascriptExecutor) driver;
        vars = new HashMap&lt;String, Object&gt;();
    }

    @After
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void test() {
        driver.get(&quot;https://www.lufthansa.com/ma/en/homepage&quot;);
        driver.manage().window().setSize(new Dimension(1552, 840));
        driver.findElement(By.xpath(&quot;//*[@id=\&quot;cm-acceptAll\&quot;]&quot;)).click();
        driver.findElement(By.xpath(&quot;//*[@id=\&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\&quot;]&quot;)).clear();
        driver.findElement(By.xpath(&quot;//*[@id=\&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\&quot;]&quot;))
              .sendKeys(&quot;RBA&quot;); 
        driver.findElement(By.xpath(&quot;//*[@id=\&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].destinationCode\&quot;]&quot;))
              .sendKeys(&quot;JFK&quot;); 
        driver.findElement(By.xpath(&quot;//*[@id=\&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].travelDatetime\&quot;]&quot;))
              .click();
        driver.close(); 
    }
}

答案1

得分: 1

根本原因: 此XPath表达式//*[@id=&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode&quot;],您用于定位元素的ID属性是不正确的,因为ID的值是动态的。每次页面加载时,值都不同。因此,请将您的XPath表达式更改如下所示:

将这些行更改为:

driver.findElement(By.xpath(&quot;//input[@placeholder=&#39;From&#39;]&quot;)).clear();
driver.findElement(By.xpath(&quot;//input[@placeholder=&#39;To&#39;]&quot;)).sendKeys(&quot;JFK&quot;); 
英文:

Root Cause: This XPath expression //*[@id=&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode&quot;] 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(&quot;//*[@id=\&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].originCode\&quot;]&quot;)).clear();
driver.findElement(By.xpath(&quot;//*[@id=\&quot;dcep-af2044ca4-a2e1-47e3-934c-c8a6c9e14d0d-flm-flight-flightQuery.flightSegments[0].destinationCode\&quot;]&quot;))
.sendKeys(&quot;JFK&quot;); 

To:

driver.findElement(By.xpath(&quot;//input[@placeholder=&#39;From&#39;]&quot;)).clear();
driver.findElement(By.xpath(&quot;//input[@placeholder=&#39;To&#39;]&quot;)).sendKeys(&quot;JFK&quot;); 

huangapple
  • 本文由 发表于 2023年6月16日 15:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定