0
    <table class="wikitable" style="float: right;">
    <caption>Current UTC
    </caption>
    <tbody><tr>
    <td>**January 26, 2020 01:13:27 (UTC) –** <span class="plainlinks" id="purgelink"><span class="nowrap"><a class="external text" href="https://en.wikipedia.org/w/index.php?title=Coordinated_Universal_Time&amp;action=purge">Refresh</a></span></span>

</td></tr></tbody></table>

I just need to get the date. I tried:

utc = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/table[1]/tbody/tr/td/text()')

ERROR:

invalid selector: The result of the xpath expression is: [object Text]. It should be an element.

Please help.

realr
  • 3,652
  • 6
  • 23
  • 34

1 Answers1

0

Here is the fixed version of the xpath you are tying to get.

utc = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/table[1]/tbody/tr/td')
print(utc.text)

But above will include the text of the "Refresh" link. To only get the date you might try:

utc = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/table[1]/tbody/tr/td')
utc_trim = utc.get_attribute('innerHTML').split('<')[0]
print(utc_trim)

To help with page load timing issues you may want to do something like below:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

utc = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mw-content-text"]/div/table/tbody/tr/td')))
utc_trim = utc.get_attribute('innerHTML').split('<')[0]
print(utc_trim)
Jortega
  • 3,616
  • 1
  • 18
  • 21