我正在尝试执行selenium脚本。我已经定义了wait
变量,但代码仍在返回错误。我的主要问题是,我能够在我的机器上运行这个程序,但是当我尝试使用我的同事机器时。它正在抛出这个错误。我不知道为什么我给铬可执行路径。我已经安装了在我的项目中使用的所有软件包。请帮帮我
这些都是我进口的:
import os
import sys
os.system('pip3 install selenium')
os.system('pip3 install pandas')
os.system('pip3 install mysql-connector-python')
os.system('pip3 install webdriver_manager')
import time
#from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as
EC
from selenium.webdriver.common.keys import Keys
import mysql.connector
import getpass
from mysql.connector import connect, Error
import pandas as pd
import multiprocessing as mp
import csv
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-maximized')
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "chromedriver")
os.system('xattr -d com.apple.quarantine chromedriver')
driver = webdriver.Chrome(executable_path=DRIVER_BIN,
options=chrome_options)
这是我的等待变量
wait = WebDriverWait(driver, 100)
错误:
Traceback (most recent call last):
File "main.py", line 160, in <module>
tenant = wait.until(EC.visibility_of_element_located((By.NAME, "tenantsearch")))
NameError: name 'wait' is not defined
发布于 2022-07-29 19:33:29
是的,您定义了它,但是您首先导入了它吗?
from selenium.webdriver.support.ui import WebDriverWait
另外,您在调用它的文件中导入它/定义它吗?
发布于 2022-07-29 20:56:57
这个错误信息..。
NameError: name 'wait' is not defined
您试图使用的变量,即...implies,即wait
,尚未定义。
解决方案
想必wait
变量似乎是WebDriverWait的一种类型。因此,您必须首先将其定义为:
wait = WebDriverWait(driver, 20)
然后可以使用以下代码行:
tenant = wait.until(EC.visibility_of_element_located((By.NAME, "tenantsearch")))
注意事项:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
注意:在使用WebDriverWait时,请记住删除
implicitly_wait()
的所有实例,因为混合隐式和显式等待会导致不可预测的等待时间。例如,设置10秒的隐式等待和15秒的显式等待,可能会导致20秒后出现超时。
https://stackoverflow.com/questions/73169358
复制相似问题