【问题标题】:Retrieving dynamic value with selenium webdriver, python使用 selenium webdriver,python 检索动态值
【发布时间】:2020-04-01 20:36:43
【问题描述】:

我知道已经存在关于此的类似主题。但是,当尝试以前建议的方法来检索我的特定动态表值时,我得到的只是一个 nbsp 值或类似"1a207feb-8080-4ff0-..."

我想做的事:

here 获取欧元/盎司黄金的当前表值。我“检查”了页面并得到了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,但无济于事。我在这里想念什么?我只是没有正确编码这个值,还是我从错误的来源提取?

【问题讨论】:

    标签: python selenium xpath webdriver webdriverwait


    【解决方案1】:

    要提取黄金欧元/盎司值的表值,即文本 €1,452.47,您必须为 visibility_of_element_located() 诱导 WebDriverWait,您可以使用以下Locator Strategies

    • 使用 XPATHget_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
      
    • 使用 XPATHtext 属性:

      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
      

    【讨论】:

    • 感谢您的回答!由于未定义“EC”,此解决方案无法编译。您能否澄清一下 EC 是什么以及您在哪里检索到它?最好的问候
    • @paxel 查看更新的答案并让我知道状态。
    • 似乎第一个替代方案产生了“nbsp”的控制台输出,而第二个替代方案根本不打印任何内容。我已将驱动程序启动为: driver = webdriver.Chrome("path/to/chromedriver_win32/chromedriver")
    【解决方案2】:

    等待页面加载,然后尝试获取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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多