硒:在元素包含子类的元素中获取文本

时间:2019-02-28 21:23:03

标签: python selenium

我的html页面如下:

<div class="some class">
<p>
   <i class="class1"></i>
   Some Text
</p>
<p>
   <i class="class2"></i>
   Some Text
</p>
.  .  .
.  .  .
.  .  .
</div

我想获得一些文本。目前,我正在尝试:

elem = browser.find_element_by_xpath("//div[@class='some class']")
text = elem.find_element_by_xpath("//p/i[@class='class1']").text

但是它返回一个空字符串。我不明白为什么。我是硒新手。请帮忙。

4 个答案:

答案 0 :(得分:1)

您在下面使用xpath:

# Find "i" element with "class1" css class and get first parent "p" element
elem = browser.find_element_by_xpath("//i[@class='class1']/ancestor::p[1]")
# Same as previous with added "div"
elem = browser.find_element_by_xpath("//div[@class='some class']//i[@class='class1']/ancestor::p[1]")

# Find "p" element with child "i" element with "class1" css class
elem = browser.find_element_by_xpath("//p[./i[@class='class1']]")
# Same as previous with added "div"
elem = browser.find_element_by_xpath("//div[@class='some class']//p[./i[@class='class1']]")

答案 1 :(得分:0)

您的选择器正在获取具有属性i的元素class="class1"i没有文本,这就是为什么它是一个空字符串的原因,请解决此问题:

elem = browser.find_element_by_xpath("//div[@class='some class']")

# Now let's find the i element you want
i_elem = elem.find_element_by_xpath("//i[@class='class1']")

# Now find the parent of that i_elem, which is p
p_elem = [p for p in i_elem.iterancestors() if p.tag=='p'][0]

txt = p_elem.text

答案 2 :(得分:0)

您可以使用execute_script

xPath = "//div[@class='some class']"
try:
    element = driver.find_element_by_xpath(xPath)
        b1Text = driver.execute_script("return arguments[0].childNodes[2].textContent", element);
        print(b1Text)
except:
       print()

尝试更改childNodes[N]中的值,例如childNodes [2],childNodes [1]

答案 3 :(得分:0)

假设您的class1class2不同,则可以使用此CSS选择器

div.some class > p:nth-child(1)以获取其中的文本。由于文本位于<p> para标记内,因此您可以从第一个<p>标记中获取文本。

elem = browser.find_element_by_css_selector("div.some class > p:nth-child(1)")
text = elem.text

这应该使您在元素内获得文本。