1

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

3

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.

| improve this answer | |
  • 1
    Thank you very much, very complete your answer : ) – Lorena C. Estrella R. Jan 10 at 17:12
  • I would not suggest to use class when unique ID property is available. – Vishal Aggarwal Jan 14 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.