简体   繁体   English

Selenium 和 Python 为 class 名称上的 find_element 返回空字符串

[英]Selenium with Python returning empty string for find_element on class name

I am working on a web scraper to pull the current surf conditions from Surfline using Selenium, but when I attempt to pull the surf height from the "quiver-surf-height" class an empty string is returned.我正在使用 web 刮板使用 Selenium 从 Surfline 中提取当前冲浪条件,但是当我尝试从“quiver-surf-height”class 中提取冲浪高度时,会返回一个空字符串。

This is what the Surfline html looks like.这就是 Surfline html 的样子。

<span class="quiver-surf-height">3-4<sup>FT</sup></span>

This is my code.这是我的代码。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

surf = driver.find_element(By.CLASS_NAME, "quiver-surf-height").text
print (f"The contents of surf is: {surf}")

After more experimenting, I couldn't find a solution as to why Selenium could not retrieve the data from "quiver-surf-height".经过更多的实验,我找不到解决为什么 Selenium 无法从“quiver-surf-height”检索数据的方法。 However, by performing a search on an element a few levels up, "quiver-spot-forecast-summary__stat", I was able to pull all the subsequent data as text.然而,通过对一个元素执行搜索,“quiver-spot-forecast-summary__stat”,我能够将所有后续数据作为文本提取。

surf = driver.find_element(By.CLASS_NAME, "quiver-spot-forecast-summary__stat").text
print (f"The contents of surf is: {surf}")

Returns退货

The contents of surf is: Surf height
3-4ft
Waist to chest

Try with Xpath instead, right-click on the HTML element and copy the Xpath and paste it into the code below.尝试使用 Xpath,右键单击 HTML 元素并复制 Xpath 并将其粘贴到下面的代码中。

driver.find_element(By.XPATH, "INSERT XPATH HERE")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM