【问题标题】:XPath index value iterationXPath 索引值迭代
【发布时间】:2021-08-25 18:31:19
【问题描述】:
            get_div1 = driver.find_element_by_xpath("(//div[@dojoattachpoint='titleNode'])[1]")
            get_div2 = driver.find_element_by_xpath("(//div[@dojoattachpoint='titleNode'])[2]")
            get_div3 = driver.find_element_by_xpath("(//div[@dojoattachpoint='titleNode'])[3]")
            get_div4 = driver.find_element_by_xpath("(//div[@dojoattachpoint='titleNode'])[4]")
.
.
.
.
.

我是 Python-Selenium 的新手。当我使用索引时,我得到了我需要的输出......我不确定我在哪个索引上得到了我的输出,那么我该如何迭代呢?因为,我发现很难将此 //div[@dojoattachpoint='titleNode'])[4] 分配给变量并进行操作。而且我不知道该网页中有多少索引可用。

【问题讨论】:

    标签: python selenium selenium-webdriver xpath


    【解决方案1】:

    您可以将所有匹配此定位器的元素放入列表中,然后遍历列表,如下所示:

    elements = driver.find_elements_by_xpath("//div[@dojoattachpoint='titleNode']")
    for element in elements:
        #do what you need here, like this
        print(element.text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 2011-07-23
      相关资源
      最近更新 更多