如何使用SAP Fiori UI的xpath修复标识元素中的错误

问题描述 投票:0回答:2

我正在编写一个selenium脚本来识别一个元素并为它发送一个SAP接口的密钥。

这适用于SAP Fiori UI应用程序。

即使我已经识别的xpath是唯一的,当我在chrome中运行代码时它不识别元素。

以下是我的代码:

public static void main(String[] args) {
    // declaration and instantiation of objects/variables
    String baseURL = "https://mercury.abc.net/default.aspx";

    WebDriver driver;
    System.setProperty("webdriver.chrome.driver", 
    "C:\\Users\\abcde\\Desktop\\Eclipse\\Selenium 
     Practice\\libs\\chromedriver.exe");

    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("useAutomationExtension", false);
    driver = new ChromeDriver(options);

     // launch Chrome and direct it to the Base URL
    driver.get(baseURL);

    WebElement MyTimesheetButton = 
    driver.findElement(By.id("Tile_WPQ8_8_7"));
    MyTimesheetButton.click();

    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//* 
    [@id=\"WD027B\"]"))); 

    WebElement Engagement =  
    driver.findElement(By.xpath("//[@id=\"WD027B\"]"));
    Engagement.sendKeys("test");

    //close Chrome
    driver.close();

}

我应该得到预期的结果作为元素识别和数据键入元素但我得到实际结果如下:

Cannot locate an element using By.xpath: //*[@id="WD027B"]

HTML:

<iframe role="main" frameborder="0" title="The title of the hosted application within the canvas area: My Timesheet" '="" src="/nwbc/ZEP3_FIN_TM_ENTRY_DYNPRO_MSTR/?sap-client=200&amp;sap-language=EN&amp;sap-nwbc-node=page_collection&amp;sap-nwbc-version_hash=392D742742D08304E969040C8A992A95&amp;sap-theme=zcorbu" id="iFrameId_1550067729776" name="iFrameId_1550067729776" style="display: block; width: 1036px; height: 641px;">Your&#x20;browser&#x20;is&#x20;currently&#x20;configured&#x20;not&#x20;to&#x20;display&#x20;inline&#x20;

<table id="WD027B-r" class="lsTblEdf3Whl lsField--table"> <tbody> <tr> <td class="lsTblEdf3Td urBorderBox" style="vertical-align:top;"><input id="WD027B" ct="CBS" lsdata="{0:'WD027B',5:'FREETEXT',7:'WD027C',14:true,20:40,25:'CLIENT_SERVER_PREFIX',26:'F4LOOKUP',30:true,32:40,34:true,35:'VALUE1'}" class="lsTblEdf3 urBorderBox urEdf2TxtHv" value="" role="combobox" name="WD027B" style="vertical-align:top;"> </td> </tr> </tbody> </table>

</iframe>
selenium xpath css-selectors webdriver webdriverwait
2个回答
0
投票

尝试这个XPath它应该工作。

 WebElement Engagement =driver.findElement(By.xpath("//table[@id='WD027B-r']/tbody/tr/td/input[@id='WD027B']"));
    Engagement.sendKeys("test");

如果这对您有用,请告诉我。


0
投票

由于元素位于<iframe>和动态元素中,因此您需要:

  • 引导WebDriverWait以获得所需的帧并切换到它。
  • 诱导WebDriverWait以获得可点击的所需元素。
  • 您可以使用以下解决方案:
  • cssSelectornew WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='iFrameId_'][src*='FIN_TM_ENTRY_DYNPRO_MSTR']"))); WebElement Engagement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.lsField--table td.urBorderBox>input.urBorderBox[ct='CBS']")));
  • xpathnew WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@id, 'iFrameId_')][contains(@src, 'FIN_TM_ENTRY_DYNPRO_MSTR')]"))); WebElement Engagement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[contains(@class, 'lsField--table')]//td[contains(@class, 'urBorderBox')]/input[contains(@class, 'urBorderBox') and @ct='CBS']")));
© www.soinside.com 2019 - 2024. All rights reserved.