Problem extracting the correct value from specific element

Viewed 40
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import login as login
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime

x = datetime.datetime.now()
x = x.strftime("%b %d")

driver = browser = webdriver.Firefox()
driver.get("https://connect.garmin.com/modern/activities")

driver.implicitly_wait(2)

iframe = driver.find_element(By.ID, "gauth-widget-frame-gauth-widget")
driver.switch_to.frame(iframe)

driver.find_element("name", "username").send_keys(login.username)

driver.find_element("name", "password").send_keys(login.password)
driver.find_element("name", "password").send_keys(Keys.RETURN)

driver.switch_to.default_content()

driver.implicitly_wait(10)

driver.find_element("name", "search").send_keys("Reading")
driver.find_element("name", "search").send_keys(Keys.RETURN)

#element = driver.find_element(By.CLASS_NAME, "unit")
element = driver.find_element(By.XPATH, "//html/body/div[1]/div[3]/div[2]/div[3]/div/div/div[2]/ul/li/div/div[2]/span[1]")
print(element.text)

So this is my code. I am trying to get a value from "unit", or the XPATH at the bottom. Problem is, in the HTML it says that the value is "Aug 25" but when I print it in my IDE it says Sep 10. Since I am using this to check if I am extracting the correct data points I need this to work, but unsure where it goes wrong.

Image of website and HTML

There is also no other element on the page with "Sep 10" as the value in the HTML.

Array(6) [ span.unit, span.unit, span.unit, span.unit, span.unit, span.unit
 ]
​
0: <span class="unit">
​​
accessKey: ""
​​
accessKeyLabel: ""
​​
assignedSlot: null
​​
attributes: NamedNodeMap [ class="unit" ]
​​
baseURI: "https://connect.garmin.com/modern/activities?search=Reading"
​​
childElementCount: 0
​​
childNodes: NodeList [ #text
 ]
​​
children: HTMLCollection { length: 0 }
​​
classList: DOMTokenList [ "unit" ]
​​
className: "unit"
​​
clientHeight: 22
​​
clientLeft: 0
​​
clientTop: 0
​​
clientWidth: 70
​​
contentEditable: "inherit"
​​
dataset: DOMStringMap(0)
​​
dir: ""
​​
draggable: false
​​
enterKeyHint: ""
​​
firstChild: #text "Aug 25"
​​
firstElementChild: null
​​
hidden: false
​​
id: ""
​​
innerHTML: "Aug 25"
​​
innerText: "Aug 25"
​​
inputMode: ""
​​
isConnected: true
​​
isContentEditable: false
​​
lang: ""
​​
lastChild: #text "Aug 25"
​​
lastElementChild: null
​​
localName: "span"
​​
namespaceURI: "http://www.w3.org/1999/xhtml"
​​
nextElementSibling: <span class="label">​​
nextSibling: #text "\n        "
​​
nodeName: "SPAN"
​​
nodeType: 1
​​
nodeValue: null
​​
nonce: ""
​​
offsetHeight: 22
​​
offsetLeft: 50
​​
offsetParent: <li class="list-item animated row-fluid">
​​
offsetTop: 3
​​
offsetWidth: 70
​​
onabort: null
​​
onanimationcancel: null
​​
onanimationend: null
​​
onanimationiteration: null
​​
onanimationstart: null
​​
onauxclick: null
​​
onbeforeinput: null
​​
onblur: null
​​
oncanplay: null
​​
oncanplaythrough: null
​​
onchange: null
​​
onclick: null
​​
onclose: null
​​
oncontextmenu: null
​​
oncopy: null
​​
oncuechange: null
​​
oncut: null
​​
ondblclick: null
​​
ondrag: null
​​
ondragend: null
​​
ondragenter: null
​​
ondragexit: null
​​
ondragleave: null
​​
ondragover: null
​​
ondragstart: null
​​
ondrop: null
​​
ondurationchange: null
​​
onemptied: null
​​
onended: null
​​
onerror: null
​​
onfocus: null
​​
onformdata: null
​​
onfullscreenchange: null
​​
onfullscreenerror: null
​​
ongotpointercapture: null
​​
oninput: null
​​
oninvalid: null
​​
onkeydown: null
​​
onkeypress: null
​​
onkeyup: null
​​
onload: null
​​
onloadeddata: null
​​
onloadedmetadata: null
​​
onloadend: null
​​
onloadstart: null
​​
onlostpointercapture: null
​​
onmousedown: null
​​
onmouseenter: null
​​
onmouseleave: null
​​
onmousemove: null
​​
onmouseout: null
​​
onmouseover: null
​​
onmouseup: null
​​
onmozfullscreenchange: null
​​
onmozfullscreenerror: null
​​
onpaste: null
​​
onpause: null
​​
onplay: null
​​
onplaying: null
​​
onpointercancel: null
​​
onpointerdown: null
​​
onpointerenter: null
​​
onpointerleave: null
​​
onpointermove: null
​​
onpointerout: null
​​
onpointerover: null
​​
onpointerup: null
​​
onprogress: null
​​
onratechange: null
​​
onreset: null
​​
onresize: null
​​
onscroll: null
​​
onsecuritypolicyviolation: null
​​
onseeked: null
​​
onseeking: null
​​
onselect: null
​​
onselectionchange: null
​​
onselectstart: null
​​
onslotchange: null
​​
onstalled: null
​​
onsubmit: null
​​
onsuspend: null
​​
ontimeupdate: null
​​
ontoggle: null
​​
ontransitioncancel: null
​​
ontransitionend: null
​​
ontransitionrun: null
​​
ontransitionstart: null
​​
onvolumechange: null
​​
onwaiting: null
​​
onwebkitanimationend: null
​​
onwebkitanimationiteration: null
​​
onwebkitanimationstart: null
​​
onwebkittransitionend: null
​​
onwheel: null
​​
outerHTML: "<span class=\"unit\">Aug 25</span>"
​​
outerText: "Aug 25"
​​
ownerDocument: HTMLDocument https://connect.garmin.com/modern/activities?search=Reading
​​
parentElement: <div class="pull-left activity-date date-col">​​
parentNode: <div class="pull-left activity-date date-col">​​
part: DOMTokenList []
​​
prefix: null
​​
previousElementSibling: null
​​
previousSibling: #text "\n        "
​​
scrollHeight: 22
​​
scrollLeft: 0
​​
scrollLeftMax: 0
​​
scrollTop: 0
​​
scrollTopMax: 0
​​
scrollWidth: 70
​​
shadowRoot: null
​​
slot: ""
​​
spellcheck: false
​​
style: CSS2Properties(0)
​​
tabIndex: -1
​​
tagName: "SPAN"
​​
textContent: "Aug 25"
​​
title: ""
​​
<prototype>: HTMLSpanElementPrototype { … }
​
1: <span class="unit" title="0.00 km">​
2: <span class="unit" title="34:22">​
3: <span class="unit" title="--">​
4: <span class="unit" title="97 bpm">​
5: <span class="unit" title="120">
​
length: 6
1 Answers

You can use the hard wait, in case you are missing something. Visit this page to get more idea.

Use the following code to target the element.

element = driver.find_element(By.XPATH, "(//div[@class='pull-left activity-date date-col']/span[1])[1]")
print(element.text)

Also, please share the HTML Source to frame the problem better.

Related