0

I wish to used the selenium python to click the button but error, below is my python code for clicking the button:

br.find_element_by_css_selector("span[id^='rallybutton-1121-btnIconEl']").click()

OR

br.find_element_by_xpath('//*[@id="rallybutton-1121-btnIconEI"]').click()

OR

br.find_element_by_xpath('//*[@id="rallybutton-1121"]').click()

AND etc also not workable

And below is my inspect element:

<a class="x4-btn secondary rly-small x4-unselectable x4-btn-default-small x4-icon x4-btn-icon x4-btn-default-small-icon" style="float:right;border-width:0;" hidefocus="on" unselectable="on" tabindex="0" id="rallybutton-1121" role="button">
   <span id="rallybutton-1121-btnWrap" role="presentation" class="x4-btn-wrap" unselectable="on"><span id="rallybutton-1121-btnEl" class="x4-btn-button" role="presentation">

     <span id="rallybutton-1121-btnInnerEl" class="x4-btn-inner x4-btn-inner-center" unselectable="on">&nbsp;</span>

        <span role="presentation" id="rallybutton-1121-btnIconEl" class="x4-btn-icon-el icon-export " unselectable="on" style="">&nbsp;</span>
     </span>
   </span>
</a>

enter image description here

My button look like this:

enter image description here

And I wish to click the Export to CSV...

enter image description here

Error:

  Traceback (most recent call last):
  File "C:\Users\SMANE\Desktop\ShiJieTest\ShIJieRally.py", line 54, in <module>
#br.find_element_by_id('rallybutton-1121').click()#rallybutton-1084-btnIconEl
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.16299 x86_64)
10
  • Your stack-trace (line 393) suggests that you're actually using X path to locate the button web-element rather than the ID. can you show your code around your line 41? What confuses me is it looks like the selenium library itself is calling the locator for Xpath "(return self.find_element(by=By.XPATH, value = xpath))" rather than using the Id. Xpath is known to vary quite a lot even with the same element so it's better to use the id because its more stable. If that doesn't work your id might be changing for some reason so try using a different locator like Class. Commented Sep 23, 2018 at 15:51
  • I would just like to add two things: the code that actually is clicking your button in your code is shown in the stack-trace and it doesn't seem to correspond with the code you posted on top. The line 41 in your code seems to be the problem and my guess is that 'nameField.click()' defaults to locating by X path which is then causing your 'NoSuchElement' Exception because x path is an unreliable locator that changes often. Another thing I notice is that your button seems to be 'unselectable' in your inspect element window. That might be causing the exception as well. Commented Sep 23, 2018 at 15:58
  • @ShiJieTio Update the question with text based HTML Commented Sep 24, 2018 at 6:11
  • @Redacted I have updated my error Commented Sep 26, 2018 at 11:44
  • As far as I can tell, you're using an ID locator correctly in which case @cruisepandey has the best answer for you: the button id is dynamic so it changes and isnt constantly 'rallybutton-1121' thus the NoSuchElement Exception. I would recomend using Class as the selector, it tends to be more stable. Another thing thats relatively stable is the actual text of the button. I don't believe its a built in command (pretty sure links have a by-text function automatically) but I'm sure you could easily make something to find/match button text unless there are multiple buttons with the same text. Commented Sep 26, 2018 at 11:51

1 Answer 1

2

The ID rallybutton-1121 you are using in your code looks quite dynamic.

You can extract rallybutton from ID using a css selector like this :

a[id^='rallybutton']  

code would be :

br.find_element_by_css_selector("a[id^='rallybutton']").click()  

Recommendation : Check a[id^='rallybutton'] in dev tool, just to make sure there should be only one entry .

Consider adding WebDriverWait in your code for more stability.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.