当源代码没有改变,但是Selenium WebDriver没有返回元素时该怎么办?

3

我在使用Python的Selenium工具。

一个小时前,我用了可行的代码,但现在它却返回下面的错误信息:

no such element: Unable to lacate element:...

同样的代码在一小时前还可以运行。

问题出在哪里?我检查了源代码,但它仍然是相同的。

这是我的代码:

import selenium
from selenium import webdriver
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
PATH="C:\Program Files (x86)\chromedriver"
driver=webdriver.Chrome(PATH)
driver.get("https://moitane.ge/shop/5-gudvili/43-axali-xorci-da-xorcproduqti")
time.sleep(3)
#el = driver.find_element(By.XPATH, "//div[@class='style__ShopProductSubCategoryChip-sc-1bc3ssb-2 iKSeHs']")
el = driver.find_element(By.XPATH, "//li[@class='style__CategoryItem-sc-8ncu0g-2 tZtCz']//span[text()='ახალი ხილი']")

time.sleep(3)
el.click()
time.sleep(5)
ell = driver.find_element(By.XPATH, "//*[@id='main-layout']/div[5]/div[4]/div/div[1]/div/div/div/div[1]/div")

这给我报错:
  NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='main-layout']/div[5]/div[4]/div/div[1]/div/div/div/div[1]/div"}
      (Session info: chrome=89.0.4389.90)

这里是复制的元素:

'<div class="style__ProductItemWrapper-nvyr2y-0 cbyRwl product-item-wrapper"><img class="style__ProductItemImage-nvyr2y-2 gfMqx product-image" src="https://static.moitane.ge/Moitane_files/093c2492-ccf3-4515-adcf-2f610a09e473_Thumb.jpeg" style="width: 166px; height: 132.8px; cursor: pointer;"><p class="style__ProductItemText-nvyr2y-3 ebZGDv" style="cursor: pointer; height: 40px;">香蕉(重量)(进口)</p><div style="display: flex; margin-top: auto; width: 100%; align-items: flex-start;"><span class="style__ProductItemDesc-nvyr2y-4 cIkCnK" style="margin-left: auto;">1 公斤</span></div><div class="style__ProductItemActionsWrapper-nvyr2y-5 kQtpVm" style="cursor: pointer; position: relative;"><span class="style__ProductItemPrice-nvyr2y-6 hLsuWS"><span>4.10 ₾</span> </span><div style="position: absolute; top: 0px; right: 0px; height: 100%; width: 50%;"></div><span class="style__ProductItemActionButton-nvyr2y-7 bieLnj"><i class="icon-plus" style="color: rgb(146, 146, 157);"></i></span></div></div>'


添加HTML、添加您的代码、添加错误,解释该元素是否存在于DOM中,或者您需要等待它出现? - Gaj Julije
如果您的网页元素没有在驱动程序看到DOM时同时加载,您需要等待该元素。这可能是您的情况。在调用元素之前,可以使用thred.sleep(5000)来确保没有问题。如果有问题,请使用预期条件。 - Gaj Julije
在Java中,时间单位是毫秒,在Python中是秒。等待5到10秒钟,然后告诉我是否看到了错误。 - Gaj Julije
是的,仍然出现错误。 - Sht
是的,这个'//li[@class='style__CategoryItem-sc-8ncu0g-2 tZtCz']//span[text()='ახალი ხილი']'可以工作,但是//*[@id='main-layout']/div[5]/div[4]/div/div[1]/div/div/div/div[1]/div会给我错误。 - Sht
显示剩余12条评论
3个回答

0
子div的数量将会随时变化,您需要手动在代码中进行编辑。 相反,尝试使用缩短的xpath,例如:
//div[contains(@class, 'ProductItemWrapper') and contains(.,'ბანანი')]

包含(@text, 'text') 和包含(.,'text') 是相同的。

或者如果你只想要第一个元素,添加 [1]

//div[contains(@class, 'ProductItemWrapper')][1]

0

首先尝试使用webdriver-manager包:

pip install webdriver-manager

代码应该长这样:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

该模块会自动下载驱动程序并将其存储在Selenium已知的路径中。它将解决未来可能出现的所有与驱动程序路径相关的问题。

其次,请提供箭头指向的元素截图,并准确地突出显示它。

第三点,不要使用绝对路径来定位元素,最好使用相对路径,因为DOM可能会发生变化,而绝对路径提供了硬关联。


0
尝试使用Webdriver等待而不是sleep:
# Wait for initialize, in seconds
wait = WebDriverWait(browser, 10)

ell = wait.until(EC.visibility_of_element_located((BBy.XPATH, "//*[@id='main-layout']/div[5]/div[4]/div/div[1]/div/div/div/div[1]/div")))

你还应该努力选择更好(意思是不那么脆弱)的定位器。在这种情况下,你可以使用类似于这样的东西:

//div[contains(@class,'product-item-wrapper')]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接