Twentymiles
Twentymiles

Reputation: 11

Unable to locate element with xPath

I'm literally going crazy to find an element in a specific web page. It's an "Enter" button but I haven't been being able to locate it.

ERROR MESSAGE:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"input"}

(Session info: chrome=86.0.4240.111)

I'm going to share with you the possible selectors by ChroPath Extension:

ChroPathSelectors

ChroPathSelector(1)

HTML INSPECT CODE "ENTER" BUTTON:

<input type="button" value="Entra" onclick="parent.location.href='site.site.site'">

1°ATTEMPT - PYTHON CODE WITH REL XPATH:

elem = browser.find_element_by_xpath('//body[1]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody[1]/tr[1]/td[2]/table[1]/tbody[1]/tr[3]/td[1]/input[1]')

2°ATTEMPT - PYTHON CODE WITH ABS XPATH:

elem = browser.find_element_by_xpath('/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody[1]/tr[1]/td[2]/table[1]/tbody[1]/tr[3]/td[1]/input[1]')

I have checked if there were any iframes, but I can't find them. Please, help me.

Upvotes: 0

Views: 1214

Answers (3)

KinderAndry
KinderAndry

Reputation: 41

I agree with @josifoski. It is better to use custom xpath than autogenerated by browser. So in your case try to use the following xpath: xpath = '//input[@type="button"][@value="Entra"]'

It will be easier to understand and support.

Upvotes: 3

Twentymiles
Twentymiles

Reputation: 11

I got it.

I was wrong when I told you that there weren't any iframes in the source code.

There are 2 ones!

So this is the right code to click the "Enter" button:

browser.switch_to.frame(0)
browser.switch_to.frame(1)
elem = browser.find_element_by_css_selector('td:nth-child(2) input')
elem.click()

To figure out, I recorded my steps with Selenium Ide for Chrome Browser, then I exported the python source code and I saw 2 switch_to.frame functions; then I did 2 plus 2...

Like you can see, at the end, I used a css_selector argument based on the recorded source code that I had exported.

I hope it's clear and useful for other people.

Thank you so much for your help and sorry for the incomplete information.

Upvotes: 1

josifoski
josifoski

Reputation: 1726

Can you try this

xpath = '//input[@value="Entra"]'

Upvotes: 0

Related Questions