2

EDIT: I figured out the culprit. That extra 's' certainly didn't do me any favors but the real issue was that I needed to switch to a new frame before searching for my element. Works like a charm now. Thanks for all the help!


I'm trying to pull the number of total results from various text searches in ServiceNow. I don't have access to the API so I'm having to brute force it a bit. Right now I have a python script that's using selenium to run the search. My problem is that for the life of me I can't scrape the results.

Here's the snippet of my code that isn't working:

elm_result = web_driver.find_elements_by_name("ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d")
print("Total results: ", elm_result.text)

And here's the html and screenshot of the page it's pulling from:

<span name="ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d" id="ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d">
&nbsp;(162 matches)&nbsp;&nbsp;&nbsp;
   <em>
      <span class="search_no_results">No matches for <a title="" class="noresultlink" href="ticket_list.do sysparm_query=123TEXTQUERY321%3DSAP%5Eactive%3Dtrue">Tickets</a>
         <span>
         </span>
      </span>
   </em>
</span>

When I run the script it seems to hang on this last little before eventually throwing out the following error:

Traceback (most recent call last):
File "...", line 35, in <module> print("Total results: ", elm_result.text)
AttributeError: 'list' object has no attribute 'text'

I've tried attacking it from a bunch of different angles but nothing seems to work. My goal is to get the "162 results" text into a variable that I can then pass onto a csv.

3

1 Answer 1

1

You need to use find_element_by_name instead of find_elements_by_name. find_elements_by_name returns a WebElement, which has an attribute text, while find_elements_by_name return a list of WebElement.

Here is examples.

elm_result = web_driver.find_element_by_name("ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d")
print("Total results: ", elm_result.text)


or you can specify an index of the list.

elm_result = web_driver.find_elements_by_name("ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d")
print("Total results: ", elm_result[0].text)


EDIT

There is another problem which is about wait time for the login. You can add thread sleep or using Selenium wait for a specific element.

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
...
...
elm_result = WebDriverWait(web_driver, 10).until(EC.visibility_of_element_located((By.NAME, 'ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d')))
print("Total results: ", elm_result.text)
Sign up to request clarification or add additional context in comments.

1 Comment

When I drop the "s" it is unable to find the element, giving the error: "Unable to locate element: {"method":"name","selector":"ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d"}" I get the same result when modified for find_by_id, find_by_css, and even find_by_xpath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.