【问题标题】:Locate an an element by the value of the precedent element - Selenium通过前面元素的值定位一个元素 - Selenium
【发布时间】:2021-03-14 14:55:00
【问题描述】:

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

<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']

【问题讨论】:

  • 请发布您的尝试。

标签: selenium selenium-webdriver xpath webdriverwait expected-condition


【解决方案1】:

要定位您需要为visibilityOfElementLocated() 诱导WebDriverWait 的元素,您可以使用以下基于Locator Strategy

  • 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
    

【讨论】:

    【解决方案2】:

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

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

    所以这里我们将首先使用您提到的值检查 td,然后检查它是否有任何带有标记 td 的后续兄弟,然后我们将查看它是否具有值为 'select' 的直接子元素输入

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-18
        • 2020-12-20
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        • 2019-08-08
        • 2019-10-13
        • 1970-01-01
        相关资源
        最近更新 更多