1

Note: Similar to Python Selenium Fails to Trigger Javascript Event

I'm working on a python script using Selenium to navigate a password-protected website. One page that I need has a table that uses Javascript OnKeyDown and OnKeyUp events to select a specific row before continuing to the next page.

In my script, I am trying to use myElement.click() to select the row, but this doesn't appear to trigger the Javascript events that the page needs. A fine dotted outline appears around the row, but the row color doesn't change and the page throws an error that I need to select a row before proceeding.

For reference, here's the script I'm using:

#!/usr/bin/python

# Setup Selenium
from selenium import webdriver
browser = webdriver.Firefox(firefox_binary='/usr/bin/firefox', executable_path="./geckodriver")
browser.implicitly_wait(10) # Continue checking for up to 10 secs before saying something doesn't exist

# Get to the login page
browser.get('https://example.com/Default.aspx')

# Login
browser.find_element_by_id('username').send_keys('mister-sir')
browser.find_element_by_id('password').send_keys('notmypassword')
browser.find_element_by_css_selector('.btn').click()

# Get to the application
browser.find_element_by_id('ctl00_Banner1_ModuleList_ctl01_lnkModule').click()
browser.find_element_by_id('ctl00_Content_rbtTopLevel25').click()
browser.find_element_by_id('ctl00_btnContinue').click()

# Select the row
browser.find_elements_by_xpath('//table[@id=\'ctl00_Content_grdFloors_grid\']/tbody/tr[3]/td').click()

#Continue
browser.find_element_by_id('ctl00_btnContinue').click() # I get an error here, as the previous line didn't select the correct row

#browser.stop()

Edit

Here's a snippet representative of the table I'm trying to select:

<div>
    <div>
        <table class="block-table " id="ctl00_Content_grdFloors_grid">
            <tr class="block-header-row">
                <th align="left" scope="col">Floor</th><th align="left" scope="col">Single - Beds</th><th align="left" scope="col">Double - Beds</th><th align="left" scope="col">Triple - Beds</th><th align="left" scope="col">Quad - Beds</th><th align="left" scope="col">Other</th>
            </tr><tr class="row-detail" RoomLocationFloorSuiteID="63" OnClick="selectGrid_gridItemClick(false, this, &#39;63&#39;, &#39;&#39;, &#39;ctl00_Content_grdFloors_txtRowID&#39;, &#39;ctl00_Content_grdFloors_txtRowAttributes&#39;)" OnMouseOver="selectGrid_gridHighlight(this, &#39;63&#39;)" OnMouseOut="selectGrid_gridUnHighlight(this, &#39;63&#39;)" OnKeyDown="selectGrid_gridKeyDown(event)" OnKeyUp="selectGrid_gridKeyUp(event, false, this, &#39;63&#39;, &#39;&#39;, &#39;ctl00_Content_grdFloors_txtRowID&#39;, &#39;ctl00_Content_grdFloors_txtRowAttributes&#39;)" TabIndex="0" aria-label="myBuilding-Floor 02">
                <td align="left">myBuilding-Floor 02</td><td align="left">1</td><td align="left">0</td><td align="left">0</td><td align="left">0</td><td align="left">0</td>
            </tr><tr class="row-detail" RoomLocationFloorSuiteID="64" OnClick="selectGrid_gridItemClick(false, this, &#39;64&#39;, &#39;&#39;, &#39;ctl00_Content_grdFloors_txtRowID&#39;, &#39;ctl00_Content_grdFloors_txtRowAttributes&#39;)" OnMouseOver="selectGrid_gridHighlight(this, &#39;64&#39;)" OnMouseOut="selectGrid_gridUnHighlight(this, &#39;64&#39;)" OnKeyDown="selectGrid_gridKeyDown(event)" OnKeyUp="selectGrid_gridKeyUp(event, false, this, &#39;64&#39;, &#39;&#39;, &#39;ctl00_Content_grdFloors_txtRowID&#39;, &#39;ctl00_Content_grdFloors_txtRowAttributes&#39;)" TabIndex="0" aria-label="myBuilding-Floor 03">
                <td align="left">myBuilding-Floor 03</td><td align="left">0</td><td align="left">0</td><td align="left">1</td><td align="left">0</td><td align="left">0</td>
            </tr><tr class="row-detail" RoomLocationFloorSuiteID="71" OnClick="selectGrid_gridItemClick(false, this, &#39;71&#39;, &#39;&#39;, &#39;ctl00_Content_grdFloors_txtRowID&#39;, &#39;ctl00_Content_grdFloors_txtRowAttributes&#39;)" OnMouseOver="selectGrid_gridHighlight(this, &#39;71&#39;)" OnMouseOut="selectGrid_gridUnHighlight(this, &#39;71&#39;)" OnKeyDown="selectGrid_gridKeyDown(event)" OnKeyUp="selectGrid_gridKeyUp(event, false, this, &#39;71&#39;, &#39;&#39;, &#39;ctl00_Content_grdFloors_txtRowID&#39;, &#39;ctl00_Content_grdFloors_txtRowAttributes&#39;)" TabIndex="0" aria-label="myBuilding-Floor 10">
                <td align="left">myBuilding-Floor 10</td><td align="left">0</td><td align="left">1</td><td align="left">0</td><td align="left">0</td><td align="left">0</td>
            </tr>
        </table>
    </div>

    <input name="ctl00$Content$grdFloors$txtRowID" type="hidden" id="ctl00_Content_grdFloors_txtRowID" />
    <input name="ctl00$Content$grdFloors$txtRowAttributes" type="hidden" id="ctl00_Content_grdFloors_txtRowAttributes" />
</div>
mister-sir
  • 124
  • 9
  • Did you try using `ActionChains`? http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains – Catalyst Jul 23 '17 at 20:42
  • @Catalyst: I messed around with it a little, but I didn't see much how it was different from `myElement.click(0)`. I'll read through the docs again, though. Any actions you were thinking of in particular? – mister-sir Jul 23 '17 at 21:57
  • I found [webdriver-click-vs-javascript-click](https://stackoverflow.com/questions/34562061/webdriver-click-vs-javascript-click) after more Googling, but the Javascript click event doesn't work either, as it doesn't generate the `mouseup` and `mousedown` events, just a `click` event (from what I understand). – mister-sir Jul 23 '17 at 23:59
  • There's `key_up` and `key_down`, which are the events that are fired in order to show the next page, according to your description of the issue... – Catalyst Jul 24 '17 at 07:31

0 Answers0