The code works fine when I run it in Pycharm and in Command line, but this Cannot find reference 'find_elements' in 'None' issue is not getting resolved. There are no suggestions when using driver variable. How can I fix this? The picture of the issue can be found here https://gyazo.com/a4cae984bfbc1e7aff0e43d380f2354b
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = None
@pytest.fixture()
def startingTest():
global driver
driver.implicitly_wait(5)
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://rahulshettyacademy.com/AutomationPractice/")
yield
driver.close()
def test_Radion(startingTest):
buttons = driver.find_elements(By.NAME, "radioButton")
buttons[2].click()
assert buttons[2].is_selected()
dropdown = driver.find_element(By.ID, "dropdown-class-example")
dropdownDD = Select(dropdown)
time.sleep(2)
listD = dropdownDD.options
print(len(listD))
dropdownDD.select_by_visible_text("Option2")
driver.find_element(By.ID, "autocomplete").send_keys("Ru")
names = driver.find_elements(By.CLASS_NAME, "ui-menu-item")
print(len(names))
for name in names:
print("I found this country", name.text)
if name.text == "Peru":
name.click()
break
assert driver.find_element(By.ID, "autocomplete").get_attribute('value') == "Peru"```
[1]: https://i.stack.imgur.com/kRkhc.png
[2]: https://i.stack.imgur.com/z2VDQ.png
CodePudding user response:
If you look at your code, your are using driver before creating it
driver.implicitly_wait(5) # Here it is still None
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe") # here you create it
that I think you would have to resolve. Apart from that, as it is a fixture, I would not declare driver as a global but just yield it from the fixture, i.e.
@pytest.fixture()
def driver(): #renamed
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(5) # Not sure about that
driver.get("https://rahulshettyacademy.com/AutomationPractice/")
yield driver
driver.close()
def test_Radion(driver):
...
