How to jump between two different elements located same level?



  • In the google chrome inspector console enter the xpath:

    $x("/html/body/div/div/footer/md-toolbar/div/div[2]/img[2]/../../../../../../..//*[@id='input_15']")
    

    the console returns the element, therefore the xpath is correct.

    But then in the python console I have the result that it does not find the element, the script is:

    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time
    driver = webdriver.Chrome()
    driver.get("http://portalapps.vtr.cl")
    parentHandle = driver.current_window_handle
    print("Handle principal: " , parentHandle)
    download = driver.find_element(By.XPATH, 
    "/html/body/div/div/footer/md-toolbar/div/div[2]/img[2]")
    if download is not None:
    print("found")
    download.click()
    wait = WebDriverWait(driver, 10)
    pass = espera.until(EC.element_to_be_clickable((By.XPATH, 
    "/html/body/div/div/footer/md- 
    toolbar/div/div[2]/img[2]/../../../../../../../body[@class='layout-column 
    md-dialog-is-showing']")))
    if pass is not None:
    print("found")
    time.sleep(10)
    driver.quit()
    

    I use Python, Selenium Web Driver and Chrome Browser

    thanks who can have any ideas



  • Your xpath locator is wrong , try the below xpath:

    /html/body/div/div/footer/md-toolbar/div/div[2]/img[2]/../../../../../../../../body[@class="layout-column md-dialog-is-showing"]
    

    The errors were ,

    1. space after /html/body/div/div/footer/md-
    2. more than one space character after body[@class="layout-column
    3. You navigated back to parent only seven times, ../../../../../../.. which brings you back to body element. From here you are again trying to get 'body' which will look for body inside body. But there won't be any such element. You have to navigate back one more time and then search for the body element

    Final:

    I am not sure why you use such a locator when you can easily get the element by just using the locator

    //body[@class="layout-column md-dialog-is-showing"]

    I hope you are trying to learn xpath and not going to push such a code

    Advice

    Try to develop best practices like using CSS, IDs , using relative XPaths, using less complex locators etc from initial learning phase only

    Else , you will pickup all the bad practices and will implement in professional life also.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2