Chrome Python selenium 崩溃

h79rfbju  于 2023-02-27  发布在  Go
关注(0)|答案(3)|浏览(173)

我用“selenium”在64位的Python3.9.6中定位信息。有时它确实输出了一些正确的结果,但是在完成了10个任务中的几个之后突然崩溃。但是有时它根本不工作。代码是这样的:

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

web = Chrome()
web.get("http://lagou.com")

web.find_element(By.XPATH, '//*[@id="cboxClose"]').click()

time.sleep(1)

web.find_element(By.XPATH, '//*[@id="search_input"]').send_keys("python", Keys.ENTER)

for i in range(1, 11):
    web.find_element(By.XPATH, f'//*[@id="jobList"]/div[1]/div[{i}]/div[1]/div[1]/div[1]/a').click()

    web.switch_to.window(web.window_handles[-1])

    details = web.find_element(By.XPATH, '//*[@id="job_detail"]/dd[2]/div').text
    print(details)

    web.close()
    web.switch_to.window(web.window_handles[0])

崩溃信息是这样的

File "/Users/xxxx/Library/Mobile Documents/com~apple~CloudDocs/xxxx/Code/Python/Project/selenium(2).py", line 20, in <module>
    details = web.find_element(By.XPATH, '//*[@id="job_detail"]/dd[2]/div').text
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="job_detail"]/dd[2]/div"}
  (Session info: chrome=97.0.4692.71)
Stacktrace:
0   chromedriver                        0x000000010fa0fe69 chromedriver + 5160553
1   chromedriver                        0x000000010f99a593 chromedriver + 4679059
2   chromedriver                        0x000000010f54e2c8 chromedriver + 172744
3   chromedriver                        0x000000010f583b62 chromedriver + 392034
4   chromedriver                        0x000000010f583d21 chromedriver + 392481
5   chromedriver                        0x000000010f5b6304 chromedriver + 598788
6   chromedriver                        0x000000010f5a13fd chromedriver + 513021
7   chromedriver                        0x000000010f5b40ab chromedriver + 589995
8   chromedriver                        0x000000010f5a1623 chromedriver + 513571
9   chromedriver                        0x000000010f576dce chromedriver + 339406
10  chromedriver                        0x000000010f578105 chromedriver + 344325
11  chromedriver                        0x000000010f9cb23e chromedriver + 4878910
12  chromedriver                        0x000000010f9e2d17 chromedriver + 4975895
13  chromedriver                        0x000000010f9e8a3f chromedriver + 4999743
14  chromedriver                        0x000000010f9e361a chromedriver + 4978202
15  chromedriver                        0x000000010f9bfbb1 chromedriver + 4832177
16  chromedriver                        0x000000010f9fffd8 chromedriver + 5095384
17  chromedriver                        0x000000010fa00161 chromedriver + 5095777
18  chromedriver                        0x000000010fa172a8 chromedriver + 5190312
19  libsystem_pthread.dylib             0x00007fff204ab8fc _pthread_start + 224
20  libsystem_pthread.dylib             0x00007fff204a7443 thread_start + 15

怎么样了?

of1yzvn4

of1yzvn41#

我已经运行你的代码很多次,是的,它崩溃后随机迭代.这是一些代码的一些软件没有完成它的执行指示.而且,我的一个运行完成了所有10迭代.所以.这支持我的理论.
所以,解决办法就是加上这个

time.sleep(3)

在循环的末尾。或者在链接之前添加这一行。这对将来的更新更好。

web.implicitly_wait(10)

你可以在selenium wait documentation阅读更多关于这个的信息

rekjcdws

rekjcdws2#

我使用了SeleniumBase Python框架来提高脚本的可靠性:运行pip install seleniumbase,然后使用pytest运行以下测试:

from seleniumbase import BaseCase

class MyTestClass(BaseCase):
    def test_base(self):
        self.open("http://lagou.com")
        self.click("#cboxClose")
        self.type("#search_input", "python\n")
        for i in range(1, 11):
            self.click('#jobList > div > div:nth-of-type(%s) a' % i)
            details = self.get_text("#job_detail > dd:nth-of-type(2) > div")
            print(details)
            self.driver.close()
            self.switch_to_window(0)

此外,由于网站是中文的,您可能会喜欢SeleniumBase的中文翻译脚本:

from seleniumbase.translate.chinese import 硒测试用例

class MyTestClass(硒测试用例):
    def test_base(self):
        self.开启("http://lagou.com")
        self.单击("#cboxClose")
        self.输入文本("#search_input", "python\n")
        for i in range(1, 11):
            self.单击('#jobList > div > div:nth-of-type(%s) a' % i)
            details = self.获取文本("#job_detail > dd:nth-of-type(2) > div")
            print(details)
            self.driver.close()
            self.切换到窗口(0)

两个文件将以相同的方式可靠地运行。

z18hc3ub

z18hc3ub3#

技巧是在两个订单之间使用wait方法,以便页面有时间完全加载,还可以使用implicit_wait:)
时间.睡眠(5)驱动程序.隐式等待(5)

相关问题