如何在Selenium(python)中提取这两个元素?

时间:2017-11-15 05:16:17

标签: python selenium

我需要提取所有"蜗牛"你好世界" m' m'并且' n'。 m和n重复,但它们有不同的“蜗牛”和“#hello world" s。

[![下] [1]] [1]。

3 个答案:

答案 0 :(得分:1)

您可以使用大多数可用的find_element_by方法来定位和元素,这里是list of them alongside with the docs

例如,您可以通过链接文字找到元素,因为它们是<a>标记:

driver.find_element_by_link_text('snail')
driver.find_element_by_link_text('Re: hello world')

或者,由于您提到了XPath,您可以使用.find_element_by_xpath

答案 1 :(得分:1)

只需使用xpath

即可
driver.find_element_by_xpath("//a[contains(text(),'snail')]")
driver.find_element_by_xpath("//a[contains(text(),'Re: hello world')]")

它可能会引用引号,因为我不太了解python

答案 2 :(得分:1)

要使用文字 "snail" 提取元素,您可以使用以下任一项:

  1. css_selector

    elem1 = driver.find_element_by_css_selector("a[href=qry.php?userid=snail]")
    
  2. xpath

    elem1 = driver.find_element_by_xpath("//a[@href='qry.php?userid=snail']")
    
  3. xpath (多个属性):

    elem1 = driver.find_element_by_xpath("//a[@href='qry.php?userid=snail' and text()='snail']")
    
  4. 要使用文字 "hello world" 提取元素,您可以使用以下任一项:

    1. css_selector

      elem2 = driver.find_element_by_css_selector("a[con.php?bid=284&id=142707]")
      
    2. xpath

      elem2 = driver.find_element_by_xpath("//a[@href='con.php?bid=284&id=142707']")
      
    3. xpath (多个属性):

      elem2 = driver.find_element_by_xpath("//a[@href='con.php?bid=284&id=142707' and contains(.,'hello world')]")