且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用python在selenium中通过部分id名称查找元素

更新时间:2023-02-19 17:07:15

查找您使用的元素:

sixth_item = driver.find_element_by_id("coption5")

要仅使用 coption 定位此元素,您可以使用以下任一定位器策略:

To locate this element only by using coption you can use can use either of the following Locator Strategies:

  • 使用 XPATHstarts-with():

sixth_item = driver.find_element_by_xpath("//*[starts-with(@id, 'coption')]")

  • 使用 XPATHcontains():

    sixth_item = driver.find_element_by_xpath("//*[contains(@id, 'coption')]")
    

  • 使用 CSS_SELECTOR^(开头的通配符):

  • Using CSS_SELECTOR and ^ (wildcard of starts-with):

    sixth_item = driver.find_element_by_css_selector("[id^='coption']")
    

  • 使用 CSS_SELECTOR*(包含的通配符):

  • Using CSS_SELECTOR and * (wildcard of contains):

    sixth_item = driver.find_element_by_css_selector("[id*='coption']")
    

  • 您可以在以下位置找到关于动态 CssSelector 的详细讨论:

    You can find a detailed discussion on dynamic CssSelectors in: