Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I click on numeric buttons (onclick Event) using selenum in python?

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)
like image 465
Hamed Baziyad Avatar asked Mar 07 '23 06:03

Hamed Baziyad


2 Answers

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')
like image 107
eLRuLL Avatar answered Apr 09 '23 15:04

eLRuLL


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()
like image 31
Hamed Baziyad Avatar answered Apr 09 '23 15:04

Hamed Baziyad