Captain Tools
Captain Tools

Reputation: 23

When I print the id it doesn't match the id in "inspect element"

I have a webpage with the following:

<span class="plugin_pagetree_children_span plugin_pagetree_current" id="childrenspan173273808-0"> <a href="/display/Cardians/Shift+Turnover?src=contextnavpagetreemode">Shift Turnover</a> </span>

and I can successfully find it by link text using st = driver.find_element_by_link_text('Shift Turnover')

but when I print the id using print('-',st.id)

The id prints out as 63cd644e-495b-4985-8f9e-7ea067a2b6f1 instead of childrenspan173273808-0.

I've also attempted to get_attribute and get_property but those aren't working either. Any hints/tips/suggestions welcomed.

Thanks in advance.

Upvotes: 2

Views: 208

Answers (2)

SeleniumUser
SeleniumUser

Reputation: 4177

You are trying to retrieve value of web element. You need to use get_attribute method which is declared inside the Web element interface. Basically this method will return the value of the specified attribute in the string format.

Solution 1:

locator= driver.find_element_by_xpath("//span[@id='childrenspan173273808']//a").get_attribute("id")
print(locator)

Solution 2:

locator=driver.find_element_by_xpath("//span[@class='plugin_pagetree_children_span plugin_pagetree_current']//a[1]").get_attribute("id")
print(locator)

Upvotes: 0

CEH
CEH

Reputation: 5909

The issue I'm seeing here is that you are trying to get the ID childrenspan173273808-0, but your selector driver.find_element_by_link_text('Shift Turnover') is locating the a element, which has no ID. That is why get_attribute is not working for you. You actually want to find the span element, which contains your desired ID.

You can use this to get the ID childrenspan173273808-0:

st = driver.find_element_by_xpath("//span[a[text()='Shift Turnover']]") # locate the span
id = st.get_attribute("id") # get its ID and print
print(id)

This XPath locates the span element that appears outside of the a element with text Shift Turnover. We query on the span which contains a element with Shift Turnover text, then call get_attribute on the span element, to retrieve your desired childrenspan173273808-0 ID.

Lastly -- the ID 63cd644e-495b-4985-8f9e-7ea067a2b6f1 that was printing out in your example was not the WebElement ID attribute, but rather, "the server-assigned opaque ID for the underlying DOM element". This is detailed in the Selenium docs on WebElement.

Upvotes: 1

Related Questions