简体   繁体   English

硒无法识别xpath

[英]Selenium doesn't recognise xpath

I am using Selenium WebDriver to get from a drop down list values. 我正在使用Selenium WebDriver从下拉列表值中获取。 Unfortunately I can't get it, because my code can't recognise the xpath. 不幸的是,我无法获取它,因为我的代码无法识别xpath。

Here's my code: 这是我的代码:

  WebElement selector = driver.findElement(By.xpath("id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select"));
  Select s = new Select(selector);
  List<WebElement> options = s.getOptions();
  for (WebElement wb : options) {
     System.out.println(wb.getText());
  }

The problem is with the 1st line (WebElement selector). 问题出在第一行(WebElement选择器)上。 In output I get something like this: 在输出中,我得到这样的东西:

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The xpath expression 'id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select' cannot be evaluated 线程“主”中的异常org.openqa.selenium.InvalidSelectorException:xpath表达式'id('search')/ x:fieldset / x:table [1] / x:tbody / x:tr [2] / x:td [1] / x:select'无法评估

I've even tried to find by name or class, but selenium still doesn't find this list. 我什至试图按名称或类别查找,但是硒仍然找不到此列表。

How to solve the problem? 如何解决问题? Thanks in advance :) 提前致谢 :)

Ganjira, This is no way to write a xpath. Ganjira,这是无法编写xpath的方法。 If you find it tough to recognize item . 如果您发现很难识别物品。 Use Selenium IDE 'Select' button. 使用Selenium IDE的“选择”按钮。

If you can provide sample of html page on which you try to find your element, it would be very helpful. 如果您可以提供尝试在其上找到元素的html页面示例,那将非常有帮助。 Anyway, try to search using css selectors eg 无论如何,请尝试使用CSS选择器进行搜索,例如

WebElement selector = driver.findElement(By.css("#search > select"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM