0

I tried to find an answer but nothing, only questions solved by frames. Selenium can't find this element by class.

HTML:

<html>class="default-theme" lang=""</html>
<body>
    <div data-v-87e3a7ca="" id="app">
        <div data-v-87e3a7ca="" class="app-wrapper">
            <div data-v-87e3a7ca="" class="inventory">
                <div class="inventory-error-modal">
                    <div class="inventory-error-modal__wrapper">
                        <div class="auth-wrapper__close">
                            <img src="/img/close.b276af38.svg" alt="close">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

Python code I tried:

try:
    wait = WebDriverWait(driver,10)
    wait.until(EC.visibility_of_element_located(By.CLASS_NAME, "inventory-error-modal"))
    driver.find_element(By.XPATH, "//div[@class='inventory-error-modal']//div[@class='inventory-error-modal__wrapper']//div[@class='auth-wrapper__close']").click()
except Exception as EXX:
print("EXX")
pass

Error:

Message: no such element: Unable to locate element: {"method":"css selector","selector":".inventory-error-modal"}
  (Session info: chrome=110.0.5481.178)

Browser search by class result: https://i.stack.imgur.com/KBAjQ.png

JeffC
  • 22,180
  • 5
  • 32
  • 55
Anonymous
  • 5
  • 5

3 Answers3

0

Your first locator looks fine given the HTML you posted. I would guess that either it's in an IFRAME or that particular popup isn't on the page, maybe another popup is. Pages can sometimes have multiple popups depending on the situation.

It looks like you're just trying to close the popup. If that's the case, you can try to click the close IMG that may exist on all the popups.

wait = WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR, "img[alt='close']")).click()
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Here many button like this, i can try Xpath but it don't work idk why //div[@class='auth-wrapper__close']//img[@alt='close'] – Anonymous Mar 04 '23 at 16:57
  • We can't help you without access to the page without more specific info. – JeffC Mar 04 '23 at 21:03
0

You need to consider a couple of things:

  • There is no need to wait for the visibility_of_element_located() in one line and invoke click in the next line.

  • Generally <div> tags aren't clickable, so ideally you should target the decendent <img>.

  • If your usecase is to click on the element instead of visibility_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.inventory-error-modal > div.inventory-error-modal__wrapper > div.auth-wrapper__close > img[alt=close]"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='inventory-error-modal']/div[@class='inventory-error-modal__wrapper']/div[@class='auth-wrapper__close']/img[@alt='close']"))).click()
      
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

Your doing your xpath a bit wrong.

You can try

//div[@class='auth-wrapper__close']

Or perhaps this

//div[@class='inventory-error-modal']/div/div/img

Austin Reed
  • 37
  • 1
  • 6