[英]Unable to locate xpath element - Selenium, Python, Chromedriver
[英]no such element: Unable to locate element using chromedriver and Selenium in production environment
我有硒chromedriver的問題,我無法弄清楚是什么導致它。 幾周前一切正常,但突然出現這個錯誤。 問題來自以下功能。
def login_(browser):
try:
browser.get("some_url")
# user credentials
user = browser.find_element_by_xpath('//*[@id="username"]')
user.send_keys(config('user'))
password = browser.find_element_by_xpath('//*[@id="password"]')
password.send_keys(config('pass'))
login = browser.find_element_by_xpath('/html/body/div[1]/div/button')
login.send_keys("\n")
time.sleep(1)
sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
sidebar.send_keys("\n")
app_submit = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/ul/li[1]/a')
app_submit.send_keys("\n")
except TimeoutException or NoSuchElementException:
raise LoginException
此功能在開發環境(macOS 10.11)中沒有問題,但在生產環境中引發以下錯誤:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="sidebar"]/ul/li[1]/a"}
(Session info: headless chrome=67.0.3396.79)
(Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee97XXX),platform=Linux 4.4.0-116-generic x86_64)
我已經在每個環境中更新了Chrome和chromedriver(分別為v67和2.40)。 我也給了它更多的時間。 time.sleep(15)
。 但問題仍然存在。 我最新的猜測是,webdriver的初始化可能無法正常工作:
def initiate_webdriver():
option = webdriver.ChromeOptions()
option.binary_location = config('GOOGLE_CHROME_BIN')
option.add_argument('--disable-gpu')
option.add_argument('window-size=1600,900')
option.add_argument('--no-sandbox')
if not config('DEBUG', cast=bool):
display = Display(visible=0, size=(1600, 900))
display.start()
option.add_argument("--headless")
else:
option.add_argument("--incognito")
return webdriver.Chrome(executable_path=config('CHROMEDRIVER_PATH'), chrome_options=option)
因為,如果Display
不工作,那么可能沒有提到的sidebar
而是其他一些按鈕。
所以我的問題是:有沒有人有類似的問題? 有沒有辦法知道驅動程序在尋找這樣一個元素時顯示的頁面是什么?
報告提供登錄后找不到元素錯誤,因此我認為登錄失敗,頁面重定向到某處。 您可以使用屏幕截圖選項獲取頁面的屏幕截圖,然后查看驅動程序加載的頁面。
driver.save_screenshot("path to save screen.jpeg")
您還可以保存原始html代碼並檢查同一頁面。
根據login_(browser)
方法的一些事情:
正如您已通過以下方式確定登錄按鈕:
login = browser.find_element_by_xpath('/html/body/div[1]/div/button')
我建議寧可調用send_keys("\\n")
通過login.click()
來幫助onclick()事件來模擬點擊Login按鈕 ,如下所示:
login = browser.find_element_by_xpath('/html/body/div[1]/div/button') login.click()
接下來,當您確定側邊欄時,可以通過以下方式觸發WebDriverWait以使元素可單擊 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="sidebar"]/ul/li[1]/a'))).click()
正如您所提到的, 您的代碼塊在macOS 10.11環境中運行良好,但在生產環境(Linux)中拋出以下錯誤 ,不同的瀏覽器很可能在不同的OS體系結構中呈現不同的HTML DOM 。 因此,您必須使用相對xpath ,而不是絕對 xpath ,如下所示:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@attribute='value']"))).click()
根據initiate_webdriver()
方法的一些事情:
根據Headless Chrome入門,參數--disable-gpu
僅適用於Windows,但不適用於Linux OS的有效配置。 所以需要刪除:
option.add_argument('--disable-gpu')
注意 :您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
每當我在這樣的Selenium中遇到奇怪的問題時,我更願意重試找到導致間歇性故障的特定元素。 一種方法是將它包裝在try-except塊周圍:
try:
sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
except NoSuchElementException:
time.sleep(10)
print("Unable to find element in first time, trying it again")
sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
您還可以將try
代碼放在一個帶有合適計數變量的循環中,以使自動化代碼正常工作。 (檢查一下 )。 根據我對JAVA的經驗,這個想法解決了多個問題。
您需要等到元素可見,否則您將收到此錯誤。 嘗試這樣的事情:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import visibility_of_element_located
from selenium.webdriver.common.by import By
TIMEOUT = 5
...
xpath = '//*[@id="sidebar"]/ul/li[1]/a'
WebDriverWait(self.selenium, TIMEOUT).until(visibility_of_element_located((By.XPATH, xpath)))
browser.find_element_by_xpath(xpath)
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.