当我通过Python尝试硒时,我无法从yahoo财务页面中提取标题。
下面是我正在使用的代码:
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.finance.yahoo.com')
driver.find_element_by_xpath("//a[@title='Industries']").click()
element_list = driver.find_elements_by_xpath("//ul[@data-test='secnav-list']//child::a")
#element_list remains empty - not sure why
for i in element_list:
print(i.get_attribute('title'))
我希望获得9个职位(金融,医疗保健,服务,公用事业,工业品,基本材料,企业集团,消费品,技术)。但是,元素列表显示为空列表,因此不会执行for循环。
我尝试了chrome inspect中的xpath。我也在线尝试了其他xpath-practice-tools,看来可以正确返回所有元素。不知道为什么它在程序中不起作用。
我尝试的另一件事是在xpath中提供过滤条件,例如
driver.find_elements_by_xpath("//ul[@data-test='secnav-list']//child::a[@title='Healthcare']")
该元素可以正确返回程序中的元素。
任何人都可以帮助我了解我在这里缺少什么吗?感谢您的帮助。
答案 0 :(得分:2)
您只需要等待即可显示菜单 explicitly with WebDriverWait
:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.finance.yahoo.com')
driver.find_element_by_xpath("//a[@title='Industries']").click()
# wait for menu to show up
wait = WebDriverWait(driver, 10)
company_menu = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-test=secnav-list]")))
titles = company_menu.find_elements_by_css_selector("li > a")
for title in titles:
print(title.get_attribute('title'))
打印:
Financial
Healthcare
Services
Utilities
Industrial Goods
Basic Materials
Conglomerates
Consumer Goods
Technology
答案 1 :(得分:1)
您无需<?php
// Put this at the top of index.php
if (file_exists('./install')) {
die('You must run and delete ./install directory');
}
就可以从yahoo财务页面中提取标题,而简单的鼠标悬停可以使WebDriverWait与{{3} }:
代码块:
click()
控制台输出:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', chrome_options=options)
driver.get("http://www.finance.yahoo.com")
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Industries")))).perform()
sub_menus = WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div>ul li>a[href*='/sector/']")))
for sub_menu in sub_menus:
print(sub_menu.text)