Javascript 如何从属性style=”的元素中提取文本;显示:无;

Javascript 如何从属性style=”的元素中提取文本;显示:无;,javascript,python-3.x,selenium-webdriver,css-selectors,display,Javascript,Python 3.x,Selenium Webdriver,Css Selectors,Display,该部分的HTML为: <div class="review-small-text"> <span class="stars-rate"> <span property="starsRating"> <i class="fa fa-star-yellow fa-star"></i> <i class="fa fa-star-yellow fa-star"></i> &

该部分的HTML为:

<div class="review-small-text">
 <span class="stars-rate">
    <span property="starsRating">
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i> 
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i> 
  </span> 
</span>
<span property="reviewRating" typeof="Rating" style="display:none;">
    <span property="ratingValue">5</span> 
    <span property="bestRating">5</span>
    <span property="worstRating">0</span>
</span> 
<span property="itemReviewed" typeof="Service" class="">Liposuction</span> </div>
from selenium import webdriver
import time
url = "myurlhere"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(3)

all_reviews_listings = driver.find_elements_by_xpath("//div[@id='tab_reviews']/div[@class='provider_all_Reviews']/div[@id='pnlReviews']/div")

for review in all_reviews_listings:
    review_rating = review.find_element_by_css_selector('div.review-small-text>span:nth-of-type(2)>span:nth-of-type(1)').text
    print("Review Rating: ", review_rating)
review_rating = driver.execute_script("""return document.querySelector(".review-small-text > span[property='reviewRating'] > span[property='ratingValue']").textContent""")
但它给了我一个空字符串。 您也尝试过这一款

'div.review-small-text>span:nth-child(2)>span:nth-child(1)'
所以我认为问题不在于css选择器。显示无正在此处创建问题。 是否有任何可能的方法提取该值?

到目前为止,我尝试过的Python源代码是:

<div class="review-small-text">
 <span class="stars-rate">
    <span property="starsRating">
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i> 
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i> 
  </span> 
</span>
<span property="reviewRating" typeof="Rating" style="display:none;">
    <span property="ratingValue">5</span> 
    <span property="bestRating">5</span>
    <span property="worstRating">0</span>
</span> 
<span property="itemReviewed" typeof="Service" class="">Liposuction</span> </div>
from selenium import webdriver
import time
url = "myurlhere"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(3)

all_reviews_listings = driver.find_elements_by_xpath("//div[@id='tab_reviews']/div[@class='provider_all_Reviews']/div[@id='pnlReviews']/div")

for review in all_reviews_listings:
    review_rating = review.find_element_by_css_selector('div.review-small-text>span:nth-of-type(2)>span:nth-of-type(1)').text
    print("Review Rating: ", review_rating)
review_rating = driver.execute_script("""return document.querySelector(".review-small-text > span[property='reviewRating'] > span[property='ratingValue']").textContent""")

下面是获取ratingValue的css

使用JavaScript:

<div class="review-small-text">
 <span class="stars-rate">
    <span property="starsRating">
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i> 
     <i class="fa fa-star-yellow fa-star"></i>  
     <i class="fa fa-star-yellow fa-star"></i> 
  </span> 
</span>
<span property="reviewRating" typeof="Rating" style="display:none;">
    <span property="ratingValue">5</span> 
    <span property="bestRating">5</span>
    <span property="worstRating">0</span>
</span> 
<span property="itemReviewed" typeof="Service" class="">Liposuction</span> </div>
from selenium import webdriver
import time
url = "myurlhere"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(3)

all_reviews_listings = driver.find_elements_by_xpath("//div[@id='tab_reviews']/div[@class='provider_all_Reviews']/div[@id='pnlReviews']/div")

for review in all_reviews_listings:
    review_rating = review.find_element_by_css_selector('div.review-small-text>span:nth-of-type(2)>span:nth-of-type(1)').text
    print("Review Rating: ", review_rating)
review_rating = driver.execute_script("""return document.querySelector(".review-small-text > span[property='reviewRating'] > span[property='ratingValue']").textContent""")
不使用JavaScript:或者您也可以这样做

driver.find_element_by_css_selector(".review-small-text > span:nth-child(2) > span[property='ratingValue']").get_attribute("textContent")

祖先标记具有属性
style=“display:none;”
,因此要提取所有审阅包装,可以使用以下解决方案:

driver.execute_script("arguments[0].removeAttribute('style')", driver.find_element_by_css_selector("div.review-small-text span[property='reviewRating'][typeof='Rating']"))
print([element.text for element in driver.find_elements_css_selector("div.review-small-text span[property='reviewRating'][typeof='Rating'] span")])

您是否正在尝试从
property=“ratingValue”
中提取5?是的,虽然呈现了html,但在python中,我尝试了相同的选择器,但得到了空文本。非常感谢,先生,它现在起作用了。我正在尝试。文本不起作用。我忘记了获取文本内容。