簡體   English   中英

如何在Selenium(python)中提取這兩個元素?

[英]How to extract the two elements in Selenium(python)?

我需要在“m”和“n”類下提取所有“蝸牛”和“你好世界”。 m和n重復,但它們有不同的“蝸牛”和“你好世界”。

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

您可以使用大多數可用的find_element_by方法來定位和元素,這里是它們與文檔一起列出的列表

例如,您可以通過鏈接文本查找元素,因為它們是<a>標記:

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

或者,既然你提到了XPath,你可以使用.find_element_by_xpath

只需使用xpath

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

它可能會引用引號,因為我不太了解python

要使用文本"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']") 

要使用文本"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')]") 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM