Reputation: 4805
I have test in selenium (using Python and FirefoxWebdriver). In test website I have table of records and I want to click on element in this table, but i have to scroll to this element and then I can click on it.
To scroll I'm using this method:
def scroll_to_element(self, element):
try:
self.driver.execute_script("return arguments[0].scrollIntoView();", element)
except Exception as e:
print 'error scrolling down web element', e
But this method scrolls to much down. Element is cca 2 lines upper and still isn't visible. Is there any other way, that scrolls corectly to the Element? I'm not experienced with these scripts at all.
Here is table:
<table id="idMainGriddata_columns" agname="data_columns" userid="1" entityidcolumnname="dc_id" editable="1" border="0" class="adodb_dbgrid" scrollx="0">
<colgroup>
<col width="25">
# ...
</colgroup>
<tbody>
<tr entityid="14" class="adodb_dbgrid_even_row">
<td class="centered" value="14">...</td>
# ...
</tr>
# ... 400x tr
</tbody>
</table>
and here is use of that method:
variable = driver.find_element_by_xpath("//table[@id='idMainGriddata_columns']/tbody/tr/td[contains(text(), '" + column + "')]")
st.scroll_to_element(self, variable)
actions = ActionChains(self.driver)
actions.move_to_element(variable)
actions.double_click(variable)
actions.perform()
Upvotes: 3
Views: 7796
Reputation: 2139
Solution 1
If you only need to click the element you just can do:
driver.find_element_by_xpath("//table[@id='idMainGriddata_columns']/tbody/tr/td[contains(text(), '" + column + "')]").click()
And if you have Js implemented for that it should scroll into it.
Solution 2 (dirty way)
If your column variable is integer:
column += 2
Solution 3
Add alignToTop to your scrollIntoView:
self.driver.execute_script("return arguments[0].scrollIntoView(true);", element)
Upvotes: 3