简体   繁体   中英

Selenium - Difficulty finding input element on page (Python)

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 ).

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() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM