简体   繁体   English

在 dom 中定位元素与父元素 selenium xpath

[英]Locating element in the dom with parent selenium xpath

I am trying to select a checkbox from table td element, but Selenium is not finding the element, this is my xpath locator "//*/td/a[contains(.,'Samsung Galaxy Note 10.1')]//parent::td[last()]/input[@type='checkbox']" .我正在尝试 select 表 td 元素中的复选框,但 Selenium 未找到该元素,这是我的 xpath 定位器"//*/td/a[contains(.,'Samsung Galaxy Note 10.1')]//parent::td[last()]/input[@type='checkbox']" I need to click on input filed with type checkbox, depending on the text in the with the Galaxy text.我需要单击带有类型复选框的输入字段,具体取决于 Galaxy 文本中的文本。

大教堂

You can use this xpath你可以使用这个 xpath

//a[contains(text(),'Samsung GALAXY Note 10.1')]/../following-sibling::td[last()]//input[@type='checkbox']

You are looking for Samsung Galaxy Note 10.1 , instead, it is Samsung GALAXY Note 10.1您正在寻找Samsung Galaxy Note 10.1 ,而不是Samsung GALAXY Note 10.1

if this is unique //a[contains(text(),'Samsung GALAXY Note 10.1')] then the XPath provided by me should work.如果这是唯一的//a[contains(text(),'Samsung GALAXY Note 10.1')]那么我提供的 XPath 应该可以工作。

The element is a JavaScript enabled element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategy :该元素是一个启用了JavaScript的元素,因此在您需要为elementToBeClickable()引入WebDriverWait的元素上单击(),您可以使用以下任一定位器策略

  • Using xpath and index :使用xpath索引

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(., 'Samsung GALAXY Note 10.1')]//following::td[12]//input[starts-with(@id, 'ContentModify') and @type='checkbox']"))).click();
  • Using xpath and last() :使用xpathlast()

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//tr[@class='ContentRowOdd']/td[.//a[contains(., 'Samsung GALAXY Note 10.1')]]//following::td[last()]//input[starts-with(@id, 'ContentModify') and @type='checkbox']"))).click();

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

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