0
votes

Error results because I am unable to obtain the correct element through xpath. Has the web handle changed? Can I click an image? I don't understand how to interpret the error.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()

#browser.get('https://ebs.lcb.state.pa.us/OA_HTML/AppsLocalLogin.jsp')
driver.get('http://www.finewineandgoodspirits.com')
assert 'Fine Wine & Good Spirits: Shop Online for Wine and Spirits in Pennsylvania' in driver.title

browser.find_element_by_xpath("xpath=//div[@id='ageVerify']/img[@alt='Yes']").click()

InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression xpath=//div[@id='ageVerify']/img[@alt='Yes'] because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type. (Session info: chrome=59.0.3071.115) (Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64)

2
First off, update your question with the HTML snipped where the element you're targeting resides. Secondly, use a different WebElement locator. Open your browser console and start using jQuery to validate if your selector is valid (e.g.: $x(//div[@id='ageVerify']/img[@alt='Yes']), or $(div#ageVerify img[alt="Yes"]) for CSS-path). Your error cannot be more verbose than that. (Unable to locate an element with the xpath exp..., which translates to, your selector's not good) - iamdanchiv

2 Answers

0
votes

There's another div in between the div and img in your Xpath, so your Xpath isn't returning anything.

Change "xpath=//div[@id='ageVerify']/img[@alt='Yes']" to "//div[@id='ageVerify']/div/img[@alt='Yes']" and you should be able to click it.

0
votes

You are using driver driver = webdriver.Chrome() to initiate the webdriver and browserbrowser.find_element_by_xpath("xpath=//div[@id='ageVerify']/img[@alt='Yes']").click() to click on image which is not correct, you have to use single variable for the code to continue.

There are three images on popup

  1. Welcome to finewineandgoodspirits"
  2. Yes
  3. No

Since you want to click on Yes to continue on site, you need to update the xpath code to click on Yes image

driver.find_element_by_xpath("//div[@id='ageVerify']//img[@alt='Yes']").click()

I have used this code and I was able to click on Yes image

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('http://www.finewineandgoodspirits.com')
assert 'Fine Wine & Good Spirits: Shop Online for Wine and Spirits in Pennsylvania' in driver.title
driver.find_element_by_xpath("//div[@id='ageVerify']//img[@alt='Yes']").click()