首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium: urllib3.exceptions.MaxRetryError: HTTPConnectionPool错误

Selenium: urllib3.exceptions.MaxRetryError: HTTPConnectionPool错误
EN

Stack Overflow用户
提问于 2022-09-25 16:43:03
回答 2查看 144关注 0票数 1

我现在正在学习Python上的QA自动化,我在启动第一个也是最简单的代码时遇到了这个错误。我尝试过几种不同的方法,但行不通。我试图关闭我的VPN,我有一个良好的互联网连接,我更新了所有的python库。

我在Windows 11 Home上使用Python 3.10,pytest 7.1.3,pytest-selenium 4.0.0,selenium 4.4.3,Pycharm 2022.2.2。

这是我想要启动的代码。这个错误发生在google页面打开后,它不会将测试文本输入搜索字段,然后是urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost',port=63146):error发生。

代码语言:javascript
复制
import time
from selenium import webdriver

driver = webdriver.Chrome()


def test_search_example(selenium):
    """ Search some phrase in google and make a screenshot of the page. """

    # Open google search page:
    selenium.get('https://google.com')

    # time.sleep(5)  

    # Find the field for search text input:
    search_input = driver.find_element("name", "q")

    # Enter the text for search:
    search_input.clear()
    search_input.send_keys('first test')

    time.sleep(5) 

    # Click Search:
    search_button = driver.find_element("name", "btnK")
    search_button.click()

    time.sleep(10)  

    # Make the screenshot of browser window:
    selenium.save_screenshot('result.png')

driver.quit()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-09-25 16:56:02

您的代码有一个问题。

您创建了函数test_search_example(),但从未调用它。

这应该能起作用:

代码语言:javascript
复制
import time
from selenium import webdriver

def test_search_example():
    driver = webdriver.Chrome()
    # Open google search page:
    driver.get('https://google.com')

    time.sleep(5)  

    # Find the field for search text input:
    search_input = driver.find_element("name", "q")

    # Enter the text for search:
    search_input.clear()
    search_input.send_keys('first test')

    time.sleep(5) 

    # Click Search:
    search_button = driver.find_element("name", "btnK")
    search_button.click()

    time.sleep(10)  

    # Make the screenshot of browser window:
    driver.save_screenshot('result.png')

    driver.quit()
    
test_search_example()
票数 0
EN

Stack Overflow用户

发布于 2022-09-25 17:37:15

您似乎将来自pytest-seleniumpytest-selenium与独立于此的driver混合在一起。

请注意,您使用selenium变量导航到该URL:

代码语言:javascript
复制
selenium.get('https://google.com')

但是,您使用driver变量执行了一个操作:

代码语言:javascript
复制
search_input = driver.find_element("name", "q")
search_button = driver.find_element("name", "btnK")

因此,将find_element行更改为:

代码语言:javascript
复制
search_input = selenium.find_element("name", "q")
# and this line:
search_button = selenium.find_element("name", "btnK")

然后删除包含driver的所有其他行。

代码语言:javascript
复制
### Remove these lines:
# from selenium import webdriver
# driver = webdriver.Chrome()
# driver.quit()

然后,...and与pytest一起运行,以便在所有selenium操作中使用来自pytest-seleniumselenium夹具。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73846215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档