1

I am trying to extract some attributes from an element on a website but I am getting Unable to find element error. The element doesn't have much info in it to search, apart from the attributes, and the one that I want will change every time.

I was initially converting the HTML to text and then regex searching the text to get what I want. Now I'm trying to make it a bit more elegant.

I have tried: browser.find_element_by_name("Status")

  • to search text attribute (I know this is wrong) browser.find_element_by_xpath("//label[@class='copySource']")

  • and a bunch of other variations browser.find_element_by_xpath("//element[@attribute='Device Status:']")

plus some other stuff.

using this, with find all elements, returned a blank list without an error. browser.find_elements_by_xpath("//span[@class='class']")

The code I am trying to search:

<td>
    <label class="copySource">Device Registration Status:</label>
</td>
<tr>
    <td>
        <label class="copySource">Device Status:</label>
    </td>
    <td>
        <span class="copySource copyEndLine">operational</span>
    </td>
</tr>

I am trying to extract the attribute in the last element, the word "operational" (this word changes). Getting error (or something similar to):

NoSuchElementException: no such element: Unable to locate element: 
{"method":"xpath","selector":"//element[@attribute='Device Status:']"}
Ronnie
  • 992
  • 1
  • 9
  • 25
  • Check if the table is there in Iframe. Usually it should work with `td span.copyEndLine` css locator if the element is there in the main window. – supputuri Jun 19 '19 at 13:45
  • Na no iframes there – Glenn Davies Jun 20 '19 at 07:07
  • Found the issues - needed to drill down to specify a more accurate xpath. Did this buy copying the xpath from chrome and then pasting somewhere else. I am quite new ad didnt know i could do this. Solved! thanks for your help everyone – Glenn Davies Jun 20 '19 at 07:10
  • Glad you are able to make it. Check my answer on [how to work with the xpath](https://stackoverflow.com/questions/55870609/is-there-a-way-to-learn-xpath-without-using-firebug-or-xpath-as-firefox-is-not-s/55870909#55870909). This might give you some tips and helping hand to start with. – supputuri Jun 20 '19 at 12:12

5 Answers5

0

Try with

browser.find_element_by_css_selector(".copyEndLine").text

You might try to add a wait condition before getting the text of the element, something like "wait for element present" because maybe it is not yet loaded when you are interacting with it.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • na no luck. tried element and get the same, "no such element: Unable to locate element: {"method":"css selector","selector":".copyEndLine"}" Change it to elements and get an empty list [ ] And tried extending the wait time right out and still no luck – Glenn Davies Jun 19 '19 at 08:41
  • Is the HTML code you posted inside of an iframe by any chance? – Mate Mrše Jun 19 '19 at 10:01
0

To extract the text operational you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • cssSelector:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "tr td>span.copySource copyEndLine"))).get_attribute("innerHTML"))
    
  • xpath:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[@class='copySource' and text()='Device Status:']//following::td[1]/span[@class='copySource copyEndLine']"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Usually this happens, when there are components with similar name/tags.

Can you try: browser.find_elements_by_xpath("//span[@class='class']")[0] or any other index; based on the count of similar components on the page.

Also if you could share the url of page you are trying, it would be helpful.

Ronnie
  • 992
  • 1
  • 9
  • 25
0

The relevant XPath expression would be something like:

//label[text()='Device Status:']/ancestor::tr/descendant::*[contains(@class,'EndLine')]

where:

Demo:

enter image description here

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

You can try this:

browser.find_elements_by_cssselector(".copySource.copyEndLine")

If the element is inside a frame you have to switch to the frame, you can use this:

driver.switchTo().frame("FrameName");
Lord PK
  • 19
  • 4