硒逐班找到元素,但返回空字符串。如何修复?

发布于 2025-01-20 08:56:34 字数 578 浏览 0 评论 0 原文

该代码尝试显示一个城市的天气预报。它能够找到包含内容的类,但打印出一个空字符串。这是为什么?我如何更改代码才能不得到空字符串?

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

s=Service("C:\Program Files (x86)\chromedriver.exe")
browser = webdriver.Chrome(service=s)

city = str(input("Enter a city"))

url="https://www.weather-forecast.com/locations/"+city+"/forecasts/latest"
browser.get(url)
browser.maximize_window()

content = browser.find_element(By.CLASS_NAME, "b-forecast__table-description-content")
print(content.text)

The code tries to show the weather forecast for a city. It is able to find the class with the content, but it prints out an empty string. Why is that and how could I change my code to not get an empty string?

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

s=Service("C:\Program Files (x86)\chromedriver.exe")
browser = webdriver.Chrome(service=s)

city = str(input("Enter a city"))

url="https://www.weather-forecast.com/locations/"+city+"/forecasts/latest"
browser.get(url)
browser.maximize_window()

content = browser.find_element(By.CLASS_NAME, "b-forecast__table-description-content")
print(content.text)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2025-01-27 08:56:34

你足够近。这些内容实际上位于祖先< p> 标签的后代< span> 中。


要打印所有所需的文本,您必须诱导 webdriverwait a href =“ https://stackoverflow.com/a/64770041/7429447”> visibility_of_All_ELEments_Located() ,您可以使用以下任何一个 locator策略

  • 使用 css_selector

      city =“达拉斯”
    驱动程序。
    打印([[my_elem.text for my_elem in webdriverwait(驱动程序,20).until(ec.visibility_of_all_elements_located(((by.csss_selector)
     


  • 使用 xpath

      city =“达拉斯”
    驱动程序。
    print([[my_elem.get_attribute(“ innertext”)for my_elem在webdriverwait中(驱动程序,20).until(ec.visibility_of_all_elements_located) /span [@class ='phrase']”))))))))
     
  • 控制台输出:

      ['大雨(总雨(总计0.8英寸)),在周二之夜最重。温暖(周一下午最大86°F,在周二的夜晚最小66°F)。风降低(在阳光之夜从S起新鲜风,S -Son Night的浅风)。”,“大雨(总数为0.3英寸),大部分是在周五的早晨掉落。温暖(周三下午,最大77°F,在星期三晚上最小57°F)。风通常会轻。温暖(周六下午最大84°F,在阳光之夜最小43°F)。风减少了(阳光下的下午,北风的新风,在周一晚上平静下来)。温暖(星期三下午最大70°F,在周二的夜晚最小48°F)。风通常会轻。”]
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

You were close enough. The contents are actually within the descendant <span> of the ancestor <p> tags.


To print all the desired texts you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    city = "Dallas"
    driver.get("https://www.weather-forecast.com/locations/"+city+"/forecasts/latest")
    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "p.b-forecast__table-description-content > span.phrase")))])
    
  • Using XPATH:

    city = "Dallas"
    driver.get("https://www.weather-forecast.com/locations/"+city+"/forecasts/latest")
    print([my_elem.get_attribute("innerText") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//p[@class='b-forecast__table-description-content']/span[@class='phrase']")))])
    
  • Console Output:

    ['Heavy rain (total 0.8in), heaviest during Tue night. Warm (max 86°F on Mon afternoon, min 66°F on Tue night). Winds decreasing (fresh winds from the S on Sun night, light winds from the S by Mon night).', 'Light rain (total 0.3in), mostly falling on Fri morning. Warm (max 77°F on Wed afternoon, min 57°F on Wed night). Wind will be generally light.', 'Light rain (total 0.1in), mostly falling on Sat night. Warm (max 84°F on Sat afternoon, min 43°F on Sun night). Winds decreasing (fresh winds from the N on Sun afternoon, calm by Mon night).', 'Mostly dry. Warm (max 70°F on Wed afternoon, min 48°F on Tue night). Wind will be generally light.']
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文