Warm tip: This article is reproduced from stackoverflow.com, please click
events html launch python selenium

Executing an event from python

发布于 2020-03-27 10:19:24

As a beginner with python I am trying to make a simple automated login project. One more thing I have to do is to mouse click on the 4th row of html table to show me proper content. The html code of that segment is:

<tr class="tbl_seznam_barva_1" onclick="setTimeout('__doPostBack(\'ctl02$ctl00$BrowseSql1\',\'Select$0\')',470);" onmouseover="radekSeznamuClass=this.className;this.className='RowMouseOver';" onmouseout="this.className=radekSeznamuClass;">
  <td>virtuálny terminál</td>
</tr>

How to execute this "onclick" event?

from selenium import webdriver

#...

browser = webdriver.Firefox()

elem = browser.find_element_by_name('txtUsername')
elem.send_keys('myLogin' + Keys.RETURN)

elem = browser.find_element_by_xpath("//tr[4]")

# some code for event execution goes here...

Questioner
Dounchan
Viewed
108
Dounchan 2019-07-04 23:16

The problem is that one should wait for webpage to fully load

After the line elem.send_keys('myLogin' + Keys.RETURN) the webpage needs time to render a content, so a delay should by added:

import time

# ...

elem.send_keys('myLogin' + Keys.RETURN)
time.sleep(1)
elem=browser.find_element_by_xpath("//tr[4]")
elem.click()