我已经用python用selenium编写了一个脚本,以分析填充输入框并预装Go
按钮后填充的一些结果。目前,我的脚本可以很好地完成这一部分。但是,我的主要目标是将该容器的标题也解析为Toys & Games
。
这是我到目前为止的尝试(I could not find any idea to make a loop to do the same for all the containers
):
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = "https://www.fbatoolkit.com/"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(3)
driver.find_element_by_css_selector(".estimator-container .estimator-input").send_keys("25000",Keys.RETURN)
time.sleep(2)
item = driver.find_element_by_css_selector(".estimator-result div").text
print(item)
driver.quit()
我得到的结果:
4 (30 Days Avg)
我想要的结果:
Toys & Games
4 (30 Days Avg)
Link to an image ,您可以在其中查看它们在该站点中的外观。预期的字段还会用铅笔标记,以让您知道我要解析的字段的位置。
答案 0 :(得分:3)
尝试下面的代码以获取所需的输出
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
url = "https://www.fbatoolkit.com/"
driver = webdriver.Chrome()
driver.get(url)
for container in wait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div[class='chart-container']"))):
wait(container, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.estimator-input"))).send_keys("25000", Keys.RETURN)
title = wait(container, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".chart text"))).text
item = wait(container, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".estimator-result div"))).text
print(title, item)
driver.quit()