【问题标题】:How to fix error in identifying element using xpath for SAP Fiori UI如何修复使用 xpath for SAP Fiori UI 识别元素时的错误
【发布时间】:2019-07-06 11:17:14
【问题描述】:

我正在编写一个 selenium 脚本来识别元素并将密钥发送给它以用于 SAP 接口。

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

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

下面是我的代码:

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>

【问题讨论】:

  • 以上代码 sn-p 中没有的任何具体内容?
  • 我的意思是您提供的 ID 看起来像是一个随机值。您可以尝试使用其他属性吗。但这就是我要求提供 html 的原因。以便 OP 可以识别您无法 ATM .
  • 您的“表格”元素是否在框架/iframe 元素中?
  • 是的,它在 iframe 内

标签: selenium xpath css-selectors webdriver webdriverwait


【解决方案1】:

试试这个 XPath 它应该可以工作。

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

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

【讨论】:

  • 它没有工作:(它仍然说找不到元素
  • 你能检查一下webtable是否在任何框架内吗?
【解决方案2】:

由于该元素位于 &lt;iframe&gt;dynamic 元素中,因此您需要:

  • 诱导 WebDriverWait 使所需的框架可用并切换到它
  • 诱导 WebDriverWait 使所需的元素可点击
  • 您可以使用以下解决方案:

  • cssSelector:

    new 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']")));
    
  • xpath:

    new 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']")));
    

【讨论】:

  • 尝试了 xpath,当我在开发人员工具上提供 xpath 时,它被识别为唯一的 xpath,但是当我将它放入代码并执行时,它仍然说无法找到“无法使用 By 定位元素。 xpath:" .
  • @Komali 好消息。所以我们的定位器是正确的。现在通过inspector 检查HTML 并查看元素是否在&lt;frame&gt;&lt;iframe&gt; 内。如果是这样,您必须切换到所需的帧。
  • 是的,它在 iframe 中。我如何将它包含在我的 xpath 中?请原谅我对硒自动化的无知
  • 查看我的更新答案以找到代码以在找到元素之前切换到所需的 iframe。我为 classid 属性添加了placeholder
  • 我已经用问题中的 iframe 更新了 html sn-p
猜你喜欢
  • 1970-01-01
  • 2018-11-27
  • 2018-03-18
  • 2019-06-16
  • 1970-01-01
  • 2021-06-18
  • 2015-08-17
  • 1970-01-01
  • 2014-08-08
相关资源
最近更新 更多