1

I am aware that there already exists similar threads about this. However, when trying previously suggested methods to retrieve my specific dynamic table value, all I am getting is either a nbsp value or something cryptic like "1a207feb-8080-4ff0-..."

What I am trying to do:

Get the current table value for euro/oz value for gold from here. I "inspected" the page and got the xpath (//*[@id="bullionPriceTable"]/div/table/tbody/tr[3]/td[3]/span)

My code:

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)

This prints:

<selenium.webdriver.remote.webelement.WebElement (session="3ade114e9f0907e4eb13deac6a264fc8", element="3a670af5-8594-4504-908a-a9bfcbac7342")>

which obviously is not the number I was looking for.

I've also experimented with using get_attribute('innerHtml') and .text on the webElement, but to no avail. What am I missing here? Am I just not encoding this value correctly, or am I extracting from the wrong source?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
paxel
  • 13
  • 4

2 Answers2

1

To extract the table value for euro/oz value for gold i.e. the text €1,452.47 you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH and 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"))
    
  • Console Output:

    €1,456.30
    
  • Using XPATH and text 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']"))).text)
    
  • Console Output:

    €1,456.30
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for your answer! This solution does not compile though due to "EC" not being defined. Could you clarify what EC is and where you retrieved it? Best regards – paxel Apr 02 '20 at 13:58
  • @paxel Checkout the updated answer and let me know the status. – undetected Selenium Apr 02 '20 at 16:49
  • It seems the first alternative produces a console output of "nbsp", and the second alternative prints nothing at all. I have initiated driver as: driver = webdriver.Chrome("path/to/chromedriver_win32/chromedriver") – paxel Apr 02 '20 at 17:26
0

Wait for the page to load then try to get the innerHTML like the following example

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

0m3r
  • 12,286
  • 15
  • 35
  • 71