I'm learning QA automatization on Python right now, and I encountered that error trying to start the first and simplest code. I tried several different approaches, but it won't work. I tried to switch off my VPN, I have a good internet connection, I updated all python libraries.
I'm using Python 3.10, pytest 7.1.3, pytest-selenium 4.0.0, selenium 4.4.3, Pycharm 2022.2.2 on Windows 11 Home.
Here is the code I'm trying to launch. The error occurs after google page is open, it won't enter test text into search field and then urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=63146): error occurs.
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()
there is one issues with you code.
you made the function test_search_example()
but you never called it.
this should do the trick:
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()