How to find element value using Splinter?

时光怂恿深爱的人放手 提交于 2019-12-23 21:46:38

问题


I have following piece of html:

<p class="attrs"><span>foo:</span> <strong>foo</strong></p>
<p class="attrs"><span>bar:</span> <strong>bar</strong></p>
<p class="attrs"><span>foo2:</span> <strong></strong></p>
<p class="attrs"><span>description:</span> <strong>description body</strong></p>
<p class="attrs"><span>another foo:</span> <strong>foooo</strong></p>

I would like to get description body using splinter. I've managed to get a list of p using

browser.find_by_css("p.attrs")

回答1:


xpath = '//p[@class="attrs"]/span[text()="description:"]/following-sibling::strong'
description = browser.find_by_xpath(xpath).first.text



回答2:


Would you be able to get the description using find_by_tag?

Find by Tag

browser.find_by_tag('span')

Then go iterate through all 'span' tags and look for the value of 'description'. I used the documentation here




回答3:


You may be able to acomplish using this code, if you want to try a different approach with the selenium library:

import selenium
from selenium import webdriver
driver = webdriver.Chrome('PATH_LOCATION_TO_CHROME_DRIVER') 
driver.find_elements_by_class_name("attrs")

Hope this helps! replace PATH_LOCATION_TO_CHROME_DRIVER --- with the location of your chrome driver, if you google it should be first or second link to download and then place that download inside your Python's project folder.



来源:https://stackoverflow.com/questions/21802739/how-to-find-element-value-using-splinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!