1

I'm trying to complete the basic python Selenium demo (https://selenium-python.readthedocs.io/getting-started.html) but I'm attempting to do that same procedure on https://www.ncbi.nlm.nih.gov/. However, it seems to be returning an empty list/it can't find the element. I tried to use the class_name but that also gave me an error because the class name has a space in it.

elem = driver.find_elements_by_class_name("typeahead tt-hint")
elem2 = driver.find_elements_by_xpath('//*[@id="main_content"]/section[1]/div/div[2]/div/form/div/span/input[1]')

Also, when doing the tutorial and trying the find_elements command on the different options <input id="id-search-field" name="q" type="search" role="textbox" class="search-field" placeholder="Search" value="" tabindex="1"> I don't quite understand why I seemed to get a type <class 'selenium.webdriver.remote.webelement.WebElement'>if I used elem = driver.find_element_by_name('q') and a <type 'list'> if I used anything else (xpath, id, class_name, etc).

Thanks!

2
  • 1
    Could you please tell which element on ncbi.nlm.nih.gov you are trying to locate? I don't see any element with class_name=typeahead or with id=main_content. Also, you can try locating with just class=typeahead instead of typeahead tt-hint. To your second question, you get WebElement when you use find_element and a list when you use find_elements.
    – Shamanth
    Jan 21, 2019 at 3:33
  • Oh the element/elements issue makes sense, thanks so much! I was trying to locate the text box but I think that I may have been attempting to use to wrong tag/id? Anyway, I really appreciate the help!
    – Auraius
    Jan 21, 2019 at 5:25

1 Answer 1

1

First of all, regarding your question about the return types of the "find" commands, it's quite straightforward:

  • find_element_by_*() methods return a WebElement instance - or, in other words, a single element
  • find_elements_by_*() methods return a list of WebElement instances - or, on other words, a list of elements

Now, let's look at what specific problems you have.

I am assuming you are working with this search page as I do see the text box you are talking about on this page. Let's look at this attempt you had to find the text box:

elem = driver.find_elements_by_class_name("typeahead tt-hint")

First of all, you need to use find_element_by_class_name() as you are looking for a single element only. Also, this locator only accept single class values - so, you could use either driver.find_element_by_class_name("typeahead"), or driver.find_element_by_class_name("tt-hint").

If you need to check both of the class values, you could use a CSS selector:

driver.find_element_by_css_selector(".typeahead.tt-hint")

And, as we are looking for an input element, we could add the tag name check here as well:

driver.find_element_by_css_selector("input.typeahead.tt-hint")

But, as this element has an id element, you should just use "find element by id" strategy:

search_box = driver.find_element_by_id("search_term")
search_box.send_keys("my search query")

Make sure to look over this thread to get a better idea when to use what locator:

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.