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()
CodePudding user response:
You appear to be mixing the selenium
from pytest-selenium
with the driver
that you spun up independent of that.
Note that you navigated to the URL with the selenium
var:
selenium.get('https://google.com')
But then you performed an action with the driver
var:
search_input = driver.find_element("name", "q")
search_button = driver.find_element("name", "btnK")
So change the find_element
lines to:
search_input = selenium.find_element("name", "q")
# and this line:
search_button = selenium.find_element("name", "btnK")
and then remove all the other lines that contain driver
:
### Remove these lines:
# from selenium import webdriver
# driver = webdriver.Chrome()
# driver.quit()
...and then run with pytest
so that it uses the selenium
fixture from pytest-selenium
for all the selenium actions.
CodePudding user response:
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()