4

I have a HTML code like this:

<ul aria-hidden="false" aria-labelledby="resultsPerPage-button" id="resultsPerPage-menu" role="listbox" tabindex="0" class="ui-menu ui-corner-bottom ui-widget ui-widget-content" aria-activedescendant="ui-id-2" aria-disabled="false" style="width: 71px;">
    <li class="ui-menu-item">
        <div id="ui-id-1" tabindex="-1" role="option" class="ui-menu-item-wrapper">20</div>
   </li>
   <li class="ui-menu-item"><div id="ui-id-2" tabindex="-1" role="option" class="ui-menu-item-wrapper ui-state-active">50</div>
   </li>
   <li class="ui-menu-item"><div id="ui-id-3" tabindex="-1" role="option" class="ui-menu-item-wrapper">100</div>
   </li>
   <li class="ui-menu-item"><div id="ui-id-4" tabindex="-1" role="option" class="ui-menu-item-wrapper">200</div>
   </li>
</ul>

I want to click on "200". Can u help me? I used selenium in python 2.7

I tried doing this:

import time 

time.sleep(10) 
x=driver.find_element_by_link_text("200").click() 
x.click() 
time.sleep(8)
Hamed Baziyad
  • 1,954
  • 5
  • 27
  • 40

2 Answers2

6

The problem here is that the element that contains the text 200 is not a "Link", but only a li tag which could work as a clickable element was defined on that site.

The documentation doesn't specify it directly, but "Link" means only a tags.

The idea is the same, but you'll have to find that element on a different way than thinking about as a Link. Using xpath would be I think the best way for this approach:

x = driver.find_element_by_xpath("//div[./text()='200']")
x.click()

Now of course that would work for finding an element depending on the text it contains, but for finding the specific node you want would be even easier and better to use the id, as it should always be unique:

x = driver.find_element_by_id('ui-id-4')
eLRuLL
  • 18,488
  • 9
  • 73
  • 99
  • It is not wrong. But, without the use of send_key, it doesn't work. My HTML code shows that the button is a drop-down button which needs a combination of methods for click process. So, use of send_key() seems essential before your suggestion ( x = driver.find_element_by_id('ui-id-4')). – Hamed Baziyad Dec 27 '17 at 17:28
1

I can run it by use of "send_keys":

import time
number.click()
number.send_keys("200")
var200=driver.find_element_by_xpath("""//*[@id="ui-id-4"]""")
var200.click()
Hamed Baziyad
  • 1,954
  • 5
  • 27
  • 40