求 selenium By.xpath 包含变量 解决方案
各位大侠,请帮忙看看这个问题我用java写seleniumBy.xpath元素定位,遇到xpath里面包含有变动的ID,想通过动态获取该ID先转成string再通过By....
各位大侠, 请帮忙看看这个问题
我用java 写 selenium By.xpath 元素定位, 遇到 xpath 里面包含有变动的ID, 想通过 动态获取该ID 先转成string 再通过By.xpath 来定位, 编译报错 无法定位.
代码如下:
String tmp = "\"" + "//span[@class='bigbtn' and @eid='" + ID +"']" + "\"";
WebElement Bigbtn = driver.findElement(By.xpath(tmp));
异常如下:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector "//span[@class='bigbtn' and @eid='123']" is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression "//span[@class='bigbtn' and @eid='123']" because of the following error:
[Exception... "The expression cannot be converted to return the specified type." code: "0" nsresult: "0x805b0034 (TypeError)" location: "<unknown>"]
若直接写 常量的内容, 是可以定位的 展开
我用java 写 selenium By.xpath 元素定位, 遇到 xpath 里面包含有变动的ID, 想通过 动态获取该ID 先转成string 再通过By.xpath 来定位, 编译报错 无法定位.
代码如下:
String tmp = "\"" + "//span[@class='bigbtn' and @eid='" + ID +"']" + "\"";
WebElement Bigbtn = driver.findElement(By.xpath(tmp));
异常如下:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector "//span[@class='bigbtn' and @eid='123']" is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression "//span[@class='bigbtn' and @eid='123']" because of the following error:
[Exception... "The expression cannot be converted to return the specified type." code: "0" nsresult: "0x805b0034 (TypeError)" location: "<unknown>"]
若直接写 常量的内容, 是可以定位的 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询