python& Selenium Click和"TypeError:字符串索引必须为整数". [英] Python & Selenium Click and "TypeError: string indices must be integers"

查看:113
本文介绍了python& Selenium Click和"TypeError:字符串索引必须为整数".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一段代码,并使用该纪录片进行了许多阅读,并看了许多示例,但是仍然无法修复我的错误.我正在使用Python和Selenium Webdriver.

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.

这是问题所在:我正在尝试从使用Javascript的HTML页面提取图像.一旦我使用了Selenium Webdriver,就会显示功能调用的结果(在firefox中),但是我无法单击页面底部的数字进入第3或第4页.

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.

这是我遇到问题的HTML代码:

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>

我使用xpath功能进行了此调用:browser.find_element_by_xpath('//div[@data-reactid=".1.0.2.0.1:$2"]').click()

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()

我在寻求帮助的地方

  1. 错误
  2. 使用其他点击功能

赞赏!

推荐答案

这是已知的,当前已打开的问题 a>在硒2.49中.解决方法是,将降级到2.48 :

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"]

这篇关于python&amp; Selenium Click和"TypeError:字符串索引必须为整数".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆