當我在 Pycharm 和命令列中運行代碼時,該代碼運行良好,但是在“無”問題中找不到參考“find_elements”沒有得到解決。使用驅動程式變數時沒有建議。我怎樣才能解決這個問題?問題的圖片可以在這里找到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
uj5u.com熱心網友回復:
如果您查看您的代碼,您在創建它之前正在使用驅動程式
driver.implicitly_wait(5) # Here it is still None
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe") # here you create it
我認為你必須解決。除此之外,因為它是一個夾具,我不會將驅動程式宣告為全域,而只是從夾具中產生它,即
@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):
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/411733.html
標籤:
