When source code does not changed, but selenium webdriver deos not return the element

Viewed 147

I am working in selenium with python. I used the code that worked, one hour ago, but now it returns me that

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

The same code worked maximum one hour ago.

Where is the problem? I checked the source code, but it still the same

Here is my code:

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")

It gives me error:

  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)

Here is copied element:

'<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>'

3 Answers

The number of child divs will always change and you'll have to manually edit them in code. Instead try shortened xpaths, like these:

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

where contains(@text, 'text') and contains(.,'text') are the same.

or if you just want first element add [1]

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

First of all try to use webdriver-manager package:

pip install webdriver-manager

the code should look like this:

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

The module download driver automatically and store it at known path for selenium. It will solve all of kinda troubles with driver path what might appear in the future.

Second, please, provide screenshot of the element what you're trying to click with arrow, accurately highlighting it

Third, don't use absolute path for the elements, better relative, due to possible future changes in DOM and hard relation what provides absolute path.

Try using webdriver waits instead of sleeps:

# 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")))

You should also work on choosing better (meaning less fragile) locators. In this case you could use something like this:

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