通过先行元素的值定位一个元素 - Selenium

Locate an an element by the value of the precedent element - Selenium

我想根据前面td的值定位输入 --> 143753 考虑到我会将值 143753 放入变量中,因此我可以选择我想要的任何行 select.

<td style="width:11%;">143753</td>
<td align="right" style="width:10%;">
    <input type="submit" name="ctl00$cphContent$wucOrderPos$gvPOSSelection$ctl03$btnSelectPos" value="Select" id="cphContent_wucOrderPos_gvPOSSelection_btnSelectPos_1" class="button">
</td>

这张截图会让我想做的事情更清楚:

我的尝试:

//*[@value="Select"]//preceding-sibling::td[@text()='143753']

定位需要归纳的元素 for the visibilityOfElementLocated() and you can use the following based :

  • Java解法:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[text()='143753']//following-sibling::td[1]/input[@class='button' and @value='Select']")));
    
  • Python解法:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[text()='143753']//following-sibling::td[1]/input[@class='button' and @value='Select']")))
    
  • 注意Python解决方案):您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

如果td值为'143753',我希望你能得到输入字段的值:

//td[text()='143753']/following-sibling::td/input[@value="Select"]

所以在这里我们将首先检查 td 是否具有您提到的值,然后检查它是否有任何带有标记 td 的后续兄弟姐妹,然后我们将查看它是否具有值为 [=15 的直接子元素输入=]

Only contains(text(),'') 对我有用

//td[contains(text(),'174013')]/following-sibling::td/input[@value="Select"]