python - 用selenium,出现这个错误: WebDriverException
巴扎黑
巴扎黑 2017-04-17 17:59:05
0
2
366

问题就是搜索百度的关键词结果页面,然后随机点击这个页面的链接,我xpath是这样写的

先找到每个排名结果的区域块,
list1 = browser.find_elements_by_xpath('//*[@class="result c-container "]')

这个时候会得到一个列表,列表的每个对像就对应的一个排名的结果所在的区域块,然后在随机抽取这个列表中的块来进行点击(点击第三个排名结果):
list1[2].find_element_by_xpath('h3/a').click()

但是我再运行代码的时候,有的时候可以点击有的时候又不能点击,同样的页面~,相当不稳定,出错是这样的,百度也不能找到相关的答案


问题2,针对上一个问题的升级,我点击一个链接,就会出现新窗口(具体的搜索结果网站),我把窗口切换过去,然后随便浏览一下,在关闭窗口,然后把句柄在切换回来,这个时候,在进行点击的时候~发现又不行~~出现同样的错误,但是有的时候如果把等待时间设置长了又可以~,但是又测试又不行~
总之一句话,不稳定!!不知道高手知道这个要怎么解决?
下面是完整代码:

#coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time,datetime
from random import choice
import random

def get_ele_times(browser,times,func):
    return WebDriverWait(browser,times).until(func)

browser = webdriver.Firefox()
browser.get('https://www.baidu.com/s?wd=张特')
get_ele_times(browser,10,lambda brower: brower.find_element_by_id('content_bottom'))
now = browser.current_window_handle     #主要浏览器句柄
ranks = browser.find_elements_by_xpath('//*[@class="result c-container "]')
序号 = random.sample(range(len(ranks)), random.randint(2,5))

for i in 序号:
    ranks = browser.find_elements_by_xpath('//*[@class="result c-container "]')
    ranks[i].find_element_by_xpath('h3/a').click()
    time.sleep(1)
    allhandles = browser.window_handles
    for handle in allhandles:
        if handle != now:
            browser.switch_to_window(handle)    #切换成新窗口
    browser.close() #再关闭
    browser.switch_to_window(now)   #再切换回来
    get_ele_times(browser,10,lambda brower: brower.find_element_by_id('content_bottom'))
    time.sleep(10)
巴扎黑
巴扎黑

reply all(2)
小葫芦

I am also using selenium recently and the problem is very similar to yours. It's the same page. After finding the element, sometimes it can be clicked many times, and sometimes it won't work after a few times.

Later I found a problem, find_elements_by_xpath or id, text, css, the returned list, such as your ranks, the position of the values ​​​​in it will change, it feels like a dictionary, after using ranks[1], maybe next time Ranks[1] cannot be found. I don't know why. I printed it out and it seems that the memory address is changing. So if it is found once, the position of the elements in the list will be messed up next time, and it may not be found. .

In the end, I could only find once, click one element in the list, and remove one. After clicking the entire list, the list was empty. Reposition and then find.

There is a big problem in the middle, that is, when you are looping through this list, it is best not to sleep in the middle. If you sleep for a second like this, the elements in the list just now may have changed their positions and cannot be found. I don’t know. Why, the longer it takes, the easier it will be to mess up.

I set up that after looping the same web page 40 times, the entire web page will be refreshed and find will be repositioned. It can run continuously for 15 hours without any problem.

I tried refreshing and finding again before, but this consumes a lot of memory and CPU.

In short, the elements in the ranks list need to be constantly updated.

There is also a splinter package, which is not very useful.

黄舟

In some cases, you need to set the window size. You might as well start the driver and set it to maximize it.
I have never encountered the second problem, so I won’t talk nonsense - -

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template