当源代码未更改,但selenium webdriver deos未返回元素时

2024-10-02 16:29:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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>


Tags: fromimportdivbytimestyledriverselenium
3条回答

首先,尝试使用webdriver manager软件包:

pip install webdriver-manager

代码应该如下所示:

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

模块自动下载驱动程序并将其存储在selenium的已知路径。它将解决未来可能出现的驾驶员路径的所有问题

其次,请提供您试图用箭头单击的元素的屏幕截图,准确地突出显示它

第三,不要对元素使用绝对路径,最好是相对的,因为DOM和硬关系将来可能会发生变化,而硬关系提供了绝对路径

子div的数量将始终更改,您必须在代码中手动编辑它们。 改为尝试缩短XPath,如下所示:

//div[contains(@class, 'ProductItemWrapper') and contains(.,'ბანანი')]

其中contains(@text,'text')和contains(,'text')是相同的

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

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

尝试使用webdriver等待而不是休眠:

# 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')]

相关问题 更多 >