我目前正在研究一段代碼,並使用紀錄片完成了很多閱讀並查看了很多示例,但仍然無法修復我的錯誤。我正在使用Python和Selenium Webdriver。Python和Selenium Click和「TypeError:字符串索引必須是整數」
下面是問題:我試圖從使用Javascript的HTML頁面中提取圖像。一旦我使用selenium webdriver,就會顯示函數調用的結果(在firefox中),但是我無法點擊頁面底部的數字以進入第3或第4頁。
這裏是我有問題的HTML代碼:
<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()
我第一次這樣做,它的工作了幾下,然後將其給了我一個錯誤,我再也不能使用它了。錯誤是:
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
這裏是我使用的代碼:
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()
我所尋找的幫助:
- 錯誤
- 使用不同的功能用於點擊
欣賞它!
它是**滿**錯誤信息? – furas
@furas是msg的主要錯誤:「TypeError:字符串索引必須是整數」。我應該包含整個Traceback嗎? – BigMike