如何使用selenium和python提取值,我的最终目标是将此值存储在csv中。
我尝试过的:
#element= driver.find_element_by_xpath("//*[@class='rt-tr-group']")
elements = driver.find_elements_by_class_name("product-form__price")
for value in elements:
print(value.text)
但这会返回一个空列表吗?
HTML
看起来您错过了等待/延迟
试试这个
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".product-form__price")))
time.sleep(0.5)
elements = driver.find_elements_by_class_name("product-form__price")
for value in elements:
print(value.text)
你将需要这些进口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
并用初始化wait
对象
wait = WebDriverWait(driver, 20)
要打印内部文本,例如$53.37
,您需要诱导WebDriverWait等待可见性_of_all_elements_located((并且您可以使用以下定位器策略之一:
-
使用CLASS_NAME和
get_attribute("innerHTML")
:print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "product-form__price")))])
-
使用CSS_SELECTOR和
get_attribute("textContent")
:print([my_elem.get_attribute("textContent") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span.product-form__price")))])
-
使用XPATH和文本属性:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='product-form__price']")))])
-
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
您可以在"如何使用Selenium-Python 检索WebElement的文本"中找到相关讨论
引用
有用文档链接:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性返回The text of the element.
- 使用Selenium的文本和innerHTML之间的差异