0

I am having not luck getting a button click on a webpage.

in python coding using selenium on chrome browser.

I have found the xpath in chrome inspect.

chrome inspect xpath: NOTE Which is a looks to be a dynamic path after exportbutton-xxxx-btnInnerEl.

//*[@id="exportbutton-1102-btnInnerEl"]

I have tried 10 different xpath formats and not getting any closer to getting button selected to run report on website. My test case is just to get button pressed on this page to export some date.

Here is buttonn element in html

<span id="exportbutton-1102-btnInnerEl" class="x-btn-inner" style="height: 23px; line-height: 23px;">Export Tickets</span>

Run Error output from python in order:

submit_button = driver.find_elements_by_xpath('//*[contains(@id,’exportbutton’)]')

run errors in python

submit_button = driver.find_elements_by_xpath('//*[contains(@id,’exportbutton’)]')
return self.find_elements(by=By.XPATH, value=xpath) 'value': value})['value']
self.error_handler.check_response(response)
  raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[contains(@id,’exportbutton’)] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(@id,’exportbutton’)]' is not a valid XPath expression.
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

Thanks

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

3 Answers3

0

The apostrophes around ’exportbutton’ are not valid. And they also should be doubled to separate them from the rest of the xpath expression

submit_button = driver.find_elements_by_xpath('//*[contains(@id ,"exportbutton")]')
Guy
  • 34,831
  • 9
  • 31
  • 66
0

Using find_elements would give you list of web elements. and the exception trace says xpath is invalid because of :

If you want to click on the button , the you can refer this code :

submit_button = driver.find_elements_by_xpath("//button[contains(@id,'exportbutton')]")
submit_button[0].click()  

Hope this would help you.

cruisepandey
  • 6,860
  • 3
  • 9
  • 24
0

This error message...

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[contains(@id,’exportbutton’)] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(@id,’exportbutton’)]' is not a valid XPath expression.

......implies that the Locator Strategy you have constructed was not a valid locator.

The main reason of the error is the presence of character. Instead you sould have used the ' character within the xpath.

However as per the HTML you have provided to identify the element with text as Export Tickets you can use the following solution:

submit_button = driver.find_element_by_xpath("//span[@class='x-btn-inner' and contains(.,'Export Tickets')][starts-with(@id,'exportbutton-')]")
DebanjanB
  • 118,661
  • 30
  • 168
  • 217