0

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.

1
  • check with this elem = driver.find_element_by_xpath("//*[@id='1']/p") Commented Jun 6, 2017 at 1:14

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

1 Comment

Thanks but I solved it with adding time.sleep(3) before the driver.find_element_by_xpath(). Will try this Addon anyway!

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.