4

I am currently working on a piece of code and have done many readings using the documentary and looked at many examples, but still I cannot fix my bug. I am using Python and Selenium Webdriver.

Here's the problem: I am trying to extract an image from an HTML page that uses Javascript. The results from the function call are displayed (in firefox) once I use the selenium webdriver, but I cannot click on a number at the bottom of the page to go onto the 3rd or 4th page.

Here is the HTML code that I am having issues with:

<div class="pagebook" data-reactid=".1.0.2.0"><div style="display:none;" data-reactid=".1.0.2.0.0"></div><div class="active" data-reactid=".1.0.2.0.1:$0">1</div><div class="" data-reactid=".1.0.2.0.1:$1">2</div><div class="" data-reactid=".1.0.2.0.1:$2">3</div><div class="" data-reactid=".1.0.2.0.1:$3">4</div><div class="" data-reactid=".1.0.2.0.1:$4">5</div><div class="" dat<div class="next" style="margin-right:20px;" data-reactid=".1.0.2.0.$6">►</div></div>

I did this call using xpath capability: browser.find_element_by_xpath('//div[@data-reactid=".1.0.2.0.1:$2"]').click()

The first time I did this it worked a few times and then it gave me an error and I could never use it again. The error was:

File "C:\Python27\Lib\site-packages\selenium-2.49.2-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 111, in check_response
    message = value["value"]["message"]
TypeError: string indices must be integers

Here is the code that I am using:

browser = webdriver.Firefox()
browser.implicitly_wait(5) #backup wait of 5 seconds just in case
browser.get(url2)
browser.find_element_by_xpath('//div[@data-reactid=".1.0.2.0.1:$2"]').click()

What I am looking for help with:

  1. The error
  2. Using a different function for clicking

Appreciate it!

BigMike
  • 43
  • 5

2 Answers2

6

This is a known, currently open, issue in selenium 2.49. As a workaround, downgrade to 2.48:

pip install selenium==2.48

I would also improve the locator to:

//div[@class="pagebook"]/div[. = "2"]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I installed 2.48. When I run the program the webpage still does not change. Do you have any other ideas how to extract that from html? The error that I get for using your locator: selenium.common.exceptions.InvalidSelectorException: Message: The given selector //div[@class"pagination"]/div[. = "2"] is either invalid or does not result in a WebElement. The following error occurred:InvalidSelectorError: Unable to locate an element with the xpath expression //div[@class"pagebook"]/div[. = "2"] because of the following error: SyntaxError:The expression is not a legal expression. – BigMike Jan 23 '16 at 23:29
  • @BigMike you are missing the `=` after the `@class`. And this is not the selector I've provided. – alecxe Jan 23 '16 at 23:30
  • Now it appears to work. Do you have any other idea on how to locate that button at the bottom of the HTML page without using xpath function (find_element_by_xpath)? – BigMike Jan 23 '16 at 23:35
  • @BigMike assuming you mean the next button, this would work: `driver.find_element_by_css_selector("div.next")`. – alecxe Jan 23 '16 at 23:37
  • Yes that works, but what if I specifically wanted page "2"? Is there a way? – BigMike Jan 23 '16 at 23:48
  • @BigMike yeah, since you are checking a text of an element and it's not a link, the XPath is the best and straightforward option. – alecxe Jan 24 '16 at 00:23
  • Appreciate it a lot! – BigMike Jan 24 '16 at 00:34
  • Thank you very much, my robot framework tests were failing with this error and downgrading to 2.48 fixed me! – Ernest Mueller Jan 28 '16 at 22:35
  • The issue seems to persist in version 2.50.0. Downgrading to 2.48 solved it. – Maccesch Feb 01 '16 at 11:51
  • Perhaps it is time for an update? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Mar 05 '22 at 02:10
0

I am not seeing that error with Selenium 2.47. I did not have to change any of my Xpaths

vrp1609
  • 61
  • 1
  • 9