无法使用XPath选择链接

时间:2014-02-01 04:33:05

标签: java selenium-webdriver

我正在进行电子商务网站的自动化,因为我遇到了点击链接问题。

以下是链接

http://www.flipkart.com/mobiles/samsung~brand/pr?sid=tyy,4io&otracker=hp_nmenu_sub_electronics_0_Samsung

我无法点击可用性      排除缺货

下面是html

<input type="checkbox" value="facets.availability%5B%5D=Exclude+Out+of+Stock" class="facetoption" autocomplete="off">

使用

在xpath iam下面
   driver.findElement(By.xpath("/html/body/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div/div[2]/div[6]/ul/li/a/input")).click();


    driver.findElement(By.xpath("//input[@type='checkbox'][contains(@value,'facets.availability%5B%5D=Exclude+Out+of+Stock']")).click();

收到错误。

FAILED: FlipkartTest
org.openqa.selenium.InvalidSelectorException: The given selector //input[@type='checkbox'][contains(@value,'facets.availability%5B%5D=Exclude+Out+of+Stock'] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //input[@type='checkbox'][contains(@value,'facets.availability%5B%5D=Exclude+Out+of+Stock'] because of the following error:
[Exception... "The expression is not a legal expression."  code: "12" nsresult: "0x805b0033 (SyntaxError)"  location: "<unknown>"]
Command duration or timeout: 73 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: 'VALUED-8JGOG5PH', ip: '192.168.1.4', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45'
Session ID: f6f56eba-4608-49e0-a0c7-4bd903ae142e

我的第二个疑问是如何缩短Xpath,我不想使用像

这样的完整xpath
  driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[4]/a")).click();


        driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[5]/a")).click();


        driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[6]/a")).click();

是否可以将xpath切换到最后一个选择器,只有最后一个li正在改变。

由于

1 个答案:

答案 0 :(得分:0)

尝试以下xpath:

//li[@title='Exclude Out of Stock']/a"