Python Selenium 程序无法使用 XPATH 或 class 名称定位 html 元素。这是我显式等待的问题吗?
Python Selenium program can't locate html element using XPATH or class name. Is it a problem with my explicit wait?
我想做的事情:
为 Twitter 构建推文机器人。
我的问题:
我的程序无法找到用于输入我的用户名的文本框,即使我使用显式等待也是如此。
我试过的:
- 通过class
定位元素
- 按名称定位元素
- 通过 xpath 定位元素
- 在程序继续之前使用显式等待确保元素在屏幕上。
我的代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# getting webdriver path
s = Service('/Users/shannonslater/Desktop/Everything/Dev/WebDrivers/chromedriver101')
driver = webdriver.Chrome(service=s)
# navigating to twitter login page
driver.get('https://twitter.com/login')
# trying to click on the username text box
try:
username_text_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(By.XPATH, '//*[@id="layers"]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[5]/label/div/div[2]/div/input')
)
username_text_box.click()
except:
print("could not find username_text_box element")
# username text box is never clicked and "could not find username" is always printed
我直接从检查的 html 元素复制 xpath:
Twitter Login Page 上的 用户名 字段是一个动态元素。
<input autocapitalize="sentences" autocomplete="username" autocorrect="on" name="text" spellcheck="true" type="text" dir="auto" class="r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu" value="">
解决方案
发送一个字符序列到你需要归纳的元素WebDriverWait for the and you can use either of the following :
使用CSS_SELECTOR:
options = Options()
options.add_argument("start-maximized")
s = Service('C:\BrowserDrivers\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://twitter.com/i/flow/login")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[autocomplete='username']"))).send_keys("ShannonSlater")
使用 XPATH:
options = Options()
options.add_argument("start-maximized")
s = Service('C:\BrowserDrivers\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://twitter.com/i/flow/login")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='username']"))).send_keys("ShannonSlater")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
我想做的事情: 为 Twitter 构建推文机器人。
我的问题: 我的程序无法找到用于输入我的用户名的文本框,即使我使用显式等待也是如此。
我试过的:
- 通过class 定位元素
- 按名称定位元素
- 通过 xpath 定位元素
- 在程序继续之前使用显式等待确保元素在屏幕上。
我的代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# getting webdriver path
s = Service('/Users/shannonslater/Desktop/Everything/Dev/WebDrivers/chromedriver101')
driver = webdriver.Chrome(service=s)
# navigating to twitter login page
driver.get('https://twitter.com/login')
# trying to click on the username text box
try:
username_text_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(By.XPATH, '//*[@id="layers"]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[5]/label/div/div[2]/div/input')
)
username_text_box.click()
except:
print("could not find username_text_box element")
# username text box is never clicked and "could not find username" is always printed
我直接从检查的 html 元素复制 xpath:
Twitter Login Page 上的 用户名 字段是一个动态元素。
<input autocapitalize="sentences" autocomplete="username" autocorrect="on" name="text" spellcheck="true" type="text" dir="auto" class="r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu" value="">
解决方案
发送一个字符序列到你需要归纳的元素WebDriverWait for the
使用CSS_SELECTOR:
options = Options() options.add_argument("start-maximized") s = Service('C:\BrowserDrivers\chromedriver.exe') driver = webdriver.Chrome(service=s, options=options) driver.get("https://twitter.com/i/flow/login") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[autocomplete='username']"))).send_keys("ShannonSlater")
使用 XPATH:
options = Options() options.add_argument("start-maximized") s = Service('C:\BrowserDrivers\chromedriver.exe') driver = webdriver.Chrome(service=s, options=options) driver.get("https://twitter.com/i/flow/login") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='username']"))).send_keys("ShannonSlater")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
浏览器快照: