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!

2
  • is it full error message ? Commented Jan 23, 2016 at 21:25
  • @furas that is the main error msg: "TypeError: string indices must be integers". Should I include the whole Traceback? Commented Jan 23, 2016 at 21:28

2 Answers 2

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"]
Sign up to request clarification or add additional context in comments.

10 Comments

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 you are missing the = after the @class. And this is not the selector I've provided.
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 assuming you mean the next button, this would work: driver.find_element_by_css_selector("div.next").
Yes that works, but what if I specifically wanted page "2"? Is there a way?
|
0

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

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.