0

I'm learning with Python Selenium and I'm trying to click a button which doesn't seem to have an id. I get an error that the element could not be scrolled into view.

Could anyone help with how I click the element?

It might be useful to know that the button only appears when you mouse over a section of the page

HTML is:

<div class="btn-group" role="group" aria-label="...">
                    <button type="button" class="grey-bg btn btn-default btn-expand" title="Expand"></button>
                    <button type="button" class="grey-bg btn btn-default btn-export" title="Export"></button>
                    <button type="button" class="grey-bg btn btn-default btn-clone" title="Clone"></button>
                    <button type="button" class="grey-bg btn btn-default btn-stamp" title="Pin"></button>
                    <button type="button" class="grey-bg btn btn-default btn-delete" title="Delete"></button>
                </div>

I've tried to select by xpath:

driver.find_element_by_xpath('//*[@title="Export"]').click()

Thanks!

  • Doesn't ```find_element_by_xpath``` work? Plus, it would be helpful if u provide the url of the website. – Sushil Oct 04 '20 at 14:14
  • find_element_by_xpath doesn't seem to work unfortunately, unless I'm somehow pointing to the wrong element. The page is behind a log in so I can't share it unfortunately – ElCrouchoGrande Oct 04 '20 at 14:34
  • `//div/button[@title='Export']` try this xpath. `*` will consider all elements with `title=Export` attribute. additionally you can check for if element is inside iframe or not? if it is switch to frame then act on element – Dev Oct 04 '20 at 15:12

2 Answers2

0

From the error, it looks like the element hasn't been scrolled into view. In order to scroll to the element, just add this line to ur code:

element = driver.find_element_by_xpath('//*[@title="Export"]')
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()

Hope that this helps!

Sushil
  • 5,440
  • 1
  • 8
  • 26
0

As you mentioned, it appears when you mouse hover. Kindly refer to this discussion for mouse hover. And try to look for the button after then click using javascript as this doesn't need to be displayed in order to click.

exportBtn = driver.find_element_by_xpath('//*[@title="Export"]') driver.execute_script("arguments[0].click();", exportBtn)