2

I am having trouble finding a search bar on a webpage that I am trying to automate. I have tried a couple approaches but being rather new to selenium, I am not sure what more advanced locating options there are.

Breakdown:: The following is an section of code with the search bar element (corresponding to input) being highlighted

The xpath to the following highlighted section is below:

//*[@id='core-content-container']/div/div[2]/div/div[1]/nav/div/div[2]/form/ul/li[1]/input

When I try to find this element via find_element_by_xpath however, I get a NoSuchElementException (I have tried shorter xpaths but those provided the same error)

Below is the relevant code bit I am using to try to find this element:

from selenium import webdriver

driver = webdriver.Firefox()

#DNSMgr
driver.get('https://barista.cac.washington.edu/dnsmgr/#/fqdn')

driver.find_element_by_xpath("//div[@id='core-content-container']/div/div[2]/div/div[1]/nav/div/div[2]/form/ul/li[1]/input")

Since the exact line that I am trying to get at looks like this:

<input type="text" class="form-control ng-pristine ng-untouched ng-valid" ng-model="query" placeholder="Full domain name">

I then thought that maybe I could get at the element by using

driver.find_element_by_css_selector('input.form-control.ng-pristine.ng-untouched.ng-valid')

Since this is similar to the example on seleniums python tutorials in section 4.7 I thought that could do the job but that also doesn't work (I get another NoSuchElementException).

4
  • Try printing the driver's contents - are you sure you're searching the same page that's in your browser? I get redirected to a login page when I click your link, is selenium actively logging in prior to getting that page? Commented Oct 12, 2016 at 18:03
  • 1
    I have found that sometimes before a page loads completely, selenium tries to execute the next statement in my code. If you import the time library (standard on python) and do a time.sleep(2) (waits 2 seconds) after any navigation command, it will sometimes prevent these errors. I am assuming speed is not that big a deal since you are using selenium? @Saurabh Gaur provides some insight to what im kinda talking about. Commented Oct 12, 2016 at 18:52
  • @DanielleM. on my side I've automated the login so I don't get that page, you would have to be a UW student or faculty member to actually see the page that I am working on unfortunately Commented Oct 12, 2016 at 23:46
  • @BLang thanks, I will definitely explore that option. considered it but didn't know how to implement it Commented Oct 12, 2016 at 23:47

1 Answer 1

5

If you are getting NoSuchElementException as your provided exception, There could be following reasons :-

  • May be when you are going to find element, it would not be present on the DOM, So you should implement Explicit wait using WebDriverWait to wait until element is present as below :-

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div#core-content-container input.form-control[ng-model='query'][placeholder='Full domain name']")))
    
  • May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-

     wait = WebDriverWait(driver, 10)
    
    #Find frame or iframe and switch
    wait.until(EC.frame_to_be_available_and_switch_to_it(("frame/iframe id or name")))
    
    #Now find the element 
    element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div#core-content-container input.form-control[ng-model='query'][placeholder='Full domain name']")))
    
    #Once all your stuff done with this frame need to switch back to default
    driver.switch_to_default_content()
    
Sign up to request clarification or add additional context in comments.

Comments

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.