我正在编写一个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&sap-language=EN&sap-nwbc-node=page_collection&sap-nwbc-version_hash=392D742742D08304E969040C8A992A95&sap-theme=zcorbu" id="iFrameId_1550067729776" name="iFrameId_1550067729776" style="display: block; width: 1036px; height: 641px;">Your browser is currently configured not to display inline 
<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>
尝试这个XPath它应该工作。
WebElement Engagement =driver.findElement(By.xpath("//table[@id='WD027B-r']/tbody/tr/td/input[@id='WD027B']"));
Engagement.sendKeys("test");
如果这对您有用,请告诉我。
由于元素位于<iframe>
和动态元素中,因此您需要:
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']")));