Dante
Dante

Reputation: 3

Having trouble finding an element using Selenium/Python

I just want to say that I am new to Python/Selenium and I've been googling and wracking my head for hours now.

I am trying to extract a value from an element from the website we use for creating tickets at work. The thing is, the element with the ticket number has no ID and I cannot seem to be able to single it out using its other attributes.

The HTML for the elements is as follows:

<span class = "custom-field__value ng-binding ng-scope"
ng -
    if = "!$parent.editMode &amp;&amp; data.value" tooltip = ""
ng - bind - html = "((status.isCollapsed) ? (data.value | securelinky : '_blank')) || ('customField.label.noValue' | i18n)"> INC22963815 
</span>

So, I want to get the INC22963815 from the element.

I tried with

ticket = driver.find_element_by_class("custom-field__value ng-binding ng-scope")
ticket = driver.find_elements_by_class("custom-field__value ng-binding ng-scope")

for number in ticket:
    print(number.get_attribute("innerText"))

but I get nothing.

But when I tried with

ticket = driver.find_elements_by_tag_name("SPAN")

for number in ticket:
    print(number.get_attribute("innerText"))

I found the text I am looking for, but it also outputs almost everything else on the page, so it is not very useful.

So now I am resorting to asking the question myself. I need help just to single out that one element. Thank you in advance.

Upvotes: 0

Views: 78

Answers (2)

cruisepandey
cruisepandey

Reputation: 29362

I see you are using class name (that has spaces in it). Class name does not support spaces. so that's why it did not work. Moreover it's just a combination of multiple class separated by a space.

consider changing it to css selector :-

ticket = driver.find_element_by_css_selector("span.custom-field__value.ng-binding.ng-scope")

P.S :- it's not recommended to use automatic xpath or css generated by browser built in tools.

Upvotes: 1

kabanga bill
kabanga bill

Reputation: 36

Have you considered using the element's XPATH? You can use the Chropath plugin https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo, to get the element's XPath.

You can then do something like

driver.find_element_by_xpath("xpath_string_here")

Let me know if this helps!

Upvotes: 1

Related Questions