2016-07-19 70 views
1

每次在选择实时更新字段时请求标识发生更改。无法使用xpath查找实时更新字段的元素

正尝试通过的XPath

这让请求ID是源代码。

<form id="itemscreen334-33504-" class="itemscreen addMode v_334 ajaxForm form-initialized" method="POST" enctype="multipart/form-data" requestid="18310" style="visibility: visible; opacity: 1;"> 

Java代码:

WebElement form = driver.findElement(By.xpath("//form[Class='itemscreen addMode v_334 ajaxForm form-initialized')]")); 
System.out.println("form-->" + form); 
String requestNo = form.getAttribute("requestid"); 
System.out.println("requestNo----------->" + requestNo); 

硒错误:

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector //form[Class='itemscreen addMode v_334 ajaxForm form-initialized')] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //form[Class='itemscreen addMode v_334 ajaxForm form-initialized')] because of the following error: [Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: ""]

?我怎样才能使用XPath请求ID?

+0

XPath中的一个问题是,你有'form [Class'而不是'form [@ class',但你最好使用CSS选择器,如下面的答案。它更灵活。 – JeffC

回答

0

其实你提供错误xpath,你xpath表情貌似cssSelector,如下尝试: -

WebElement form = driver.findElement(By.cssSelector("form.itemscreen.addMode.v_334.ajaxForm.form-initialized")); 
String requestNo = form.getAttribute("requestid"); 
System.out.println("requestNo----------->" + requestNo); 

或者,如果你想使用xpath尝试用正确xpath如下: -

WebElement form = driver.findElement(By.xpath("//form[@class = 'itemscreen addMode v_334 ajaxForm form-initialized']")); 
String requestNo = form.getAttribute("requestid"); 
System.out.println("requestNo----------->" + requestNo); 

注意:如果您想要与class属性部分匹配,您应该使用xpath as By.xpath("//form[contains(@class, 'itemscreen addMode')]") and cssSelector as By.cssSelector("form.itemscreen.addMode")

+0

我已经使用“保存”按钮,这个CSS选择器 By.cssSelector(“按钮[类型=‘提交’] [数据-NAME =‘保存’]”) 我能找到它 为什么它当我们以这种格式输入时,不适用于表单类型 driver.findElement(By.cssSelector(“select [data-name ='Organization Type']”)); – Pradeeba

+0

不适用于这种情况。它是一个样本。 当我试图以类似的方式使用它,它无法找到。它是否应该像“form.itemscreen.addMode.v_334.ajaxForm.form-initialized” ? – Pradeeba

+0

@Pradeeba你想要什么?你的问题是关于从'form'元素获取属性'requestid' –

相关问题