使用 selenium webdriver 检索动态值,python
Retrieving dynamic value with selenium webdriver, python
我知道已经有关于此的类似线程。但是,当尝试以前建议的方法来检索我的特定动态 table 值时,我得到的只是一个 nbsp 值或一些神秘的东西,比如 "1a207feb-8080-4ff0-..."
我想做什么:
从 here 中获取 euro/oz 金币的当前 table 值。我 "inspected" 页面并得到了 xpath (//*[@id="bullionPriceTable"]/div/table/tbody/tr[3]/td[3]/span)
我的代码:
driver = webdriver.Chrome("path/to/chromedriver")
driver.get("https://www.bullionvault.com/gold-price-chart.do")
xpath = '//*[@id="bullionPriceTable"]/div/table/tbody/tr[3]/td[3]/span'
select=driver.find_element_by_xpath(xpath)
print(select)
这会打印:
<selenium.webdriver.remote.webelement.WebElement (session="3ade114e9f0907e4eb13deac6a264fc8", element="3a670af5-8594-4504-908a-a9bfcbac7342")>
这显然不是我要找的号码。
我也尝试过在 webElement 上使用 get_attribute('innerHtml') 和 .text,但无济于事。我在这里错过了什么?我只是没有正确编码这个值,还是我从错误的来源提取?
等待页面加载然后尝试获取 innerHTML
如下例
import time
from selenium import webdriver
chrome_browser = webdriver.Chrome(
executable_path=r"chromedriver.exe")
chrome_browser.get("https://www.bullionvault.com/gold-price-chart.do")
time.sleep(2)
select = chrome_browser.find_element_by_xpath(
"//*[@id='bullionPriceTable']/div/table/tbody/tr[3]/td[3]/span"
).get_attribute("innerHTML")
print(select)
€1,450.98
要提取黄金的 euro/oz 价值的 table 价值,即文本 €1,452.47 你必须诱导 WebDriverWait 用于 visibility_of_element_located()
并且您可以使用以下任一项 :
使用 XPATH 和 get_attribute()
:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get('https://www.bullionvault.com/gold-price-chart.do#')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='cookies-warning-buttons']//a[text()='Accept']"))).click()
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Live Gold Price']"))))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//th[text()='Gold Price per Ounce']//following-sibling::td[3]/span[@data-currency='EUR']"))).get_attribute("innerHTML"))
控制台输出:
€1,456.30
使用 XPATH 和 text 属性:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get('https://www.bullionvault.com/gold-price-chart.do#')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='cookies-warning-buttons']//a[text()='Accept']"))).click()
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Live Gold Price']"))))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//th[text()='Gold Price per Ounce']//following-sibling::td[3]/span[@data-currency='EUR']"))).text)
控制台输出:
€1,456.30
我知道已经有关于此的类似线程。但是,当尝试以前建议的方法来检索我的特定动态 table 值时,我得到的只是一个 nbsp 值或一些神秘的东西,比如 "1a207feb-8080-4ff0-..."
我想做什么:
从 here 中获取 euro/oz 金币的当前 table 值。我 "inspected" 页面并得到了 xpath (//*[@id="bullionPriceTable"]/div/table/tbody/tr[3]/td[3]/span)
我的代码:
driver = webdriver.Chrome("path/to/chromedriver")
driver.get("https://www.bullionvault.com/gold-price-chart.do")
xpath = '//*[@id="bullionPriceTable"]/div/table/tbody/tr[3]/td[3]/span'
select=driver.find_element_by_xpath(xpath)
print(select)
这会打印:
<selenium.webdriver.remote.webelement.WebElement (session="3ade114e9f0907e4eb13deac6a264fc8", element="3a670af5-8594-4504-908a-a9bfcbac7342")>
这显然不是我要找的号码。
我也尝试过在 webElement 上使用 get_attribute('innerHtml') 和 .text,但无济于事。我在这里错过了什么?我只是没有正确编码这个值,还是我从错误的来源提取?
等待页面加载然后尝试获取 innerHTML
如下例
import time
from selenium import webdriver
chrome_browser = webdriver.Chrome(
executable_path=r"chromedriver.exe")
chrome_browser.get("https://www.bullionvault.com/gold-price-chart.do")
time.sleep(2)
select = chrome_browser.find_element_by_xpath(
"//*[@id='bullionPriceTable']/div/table/tbody/tr[3]/td[3]/span"
).get_attribute("innerHTML")
print(select)
€1,450.98
要提取黄金的 euro/oz 价值的 table 价值,即文本 €1,452.47 你必须诱导 WebDriverWait 用于 visibility_of_element_located()
并且您可以使用以下任一项
使用 XPATH 和
get_attribute()
:from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC driver.get('https://www.bullionvault.com/gold-price-chart.do#') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='cookies-warning-buttons']//a[text()='Accept']"))).click() driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Live Gold Price']")))) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//th[text()='Gold Price per Ounce']//following-sibling::td[3]/span[@data-currency='EUR']"))).get_attribute("innerHTML"))
控制台输出:
€1,456.30
使用 XPATH 和 text 属性:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC driver.get('https://www.bullionvault.com/gold-price-chart.do#') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='cookies-warning-buttons']//a[text()='Accept']"))).click() driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Live Gold Price']")))) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//th[text()='Gold Price per Ounce']//following-sibling::td[3]/span[@data-currency='EUR']"))).text)
控制台输出:
€1,456.30