简体   繁体   中英

Select element with selenium python

Consider the following HTML:

 <div class="nav-category__col" id="category_nav_level_3" style="display: block;">
   <input type="hidden" value="1" name="videogame_type" id="videogame_type">
   <ul class="nav-category__list" adparam_name="videogame_type" role="radiogroup">
      <li class="nav-category__list-item  nav-category__list-item--selected" id="1" tabindex="-1" role="radio" aria-checked="true">
         <p class="nav-category__name">Consoles</p>
      </li>
      <li class="nav-category__list-item " id="2" tabindex="-1" role="radio" aria-checked="false">
         <p class="nav-category__name">Jogos</p>
      </li>
      <li class="nav-category__list-item " id="3" tabindex="-1" role="radio" aria-checked="false">
         <p class="nav-category__name">Acessórios</p>
      </li>
   </ul>
</div>

I want to click on <p class="nav-category__name">Consoles</p> so I tried doing this:

    elem = driver.find_element_by_xpath("//*[@id="1"]/p")
    elem.click()

I found this xpath by inspecting the element and copying the xpath from it, but it says "Unable to locate element" anyway. How could I do it right?

I don't know very much of HTML, but I am capable of finding other elements by id or by name.

I would recommend not to use time.sleep() in your script Because it pause your script for fix time duration even if your element is available to use in less then given time.

Lets say you have put time.sleep(5) So here it exactly pause your script for 5 seconds even if your element is available within 2 seconds.

Better way to overcome this situation use either Implicit Wait or Explicit Wait

Implicit Wait -

An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available.

driver.implicitly_wait(10) # seconds

Explicit Wait -

An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code.

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "id"))

Please refer this for further.

Yo don't need to know any HTML attribute for Selenium IDE,

https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/

install this exention in your Firefox Browser (It works just work with Firefox). And then open it, push the record button, and then do your scenerio. Finally stop and export your code for Python, or other language. If you look around to ide you can see any attribute type, like to 'name, id, cssSelector, Xpath...' and you can change which one you want.

Or your question,

elem = driver.find_element_by_name("q") // Name
elem = driver.find_element_by_id("q") // ID
menu = driver.find_element_by_css_selector(".nav") // CssSelector

you got it :)

find_element_by_*methods

Have a nice code :)

Well, I am dumb: I was trying to select an element before it was on the page. Solved by adding time.sleep(3) before elem = driver.find_element_by_xpath("//*[@id="1"]/p")

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