最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Selenium再次出错:NoSuchElementException: Message: no such element: Unable to locate element

selenium crifan 22862浏览 0评论

Selenium中,用代码:

visibleCheckoutElem = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//div[@class="order-summary"]//button[contains(@class, "cli-begin-checkout")]')))
logging.info("visibleCheckoutElem=%s", visibleCheckoutElem)
# time.sleep(5)
visibleCheckoutElem.click()
inputEmailElement = driver.find_element_by_xpath('//div[@class="placeholderContainer"]/input[@name="loginfmt"]')
logging.info("inputEmailElement=%s", inputEmailElement)

去查找元素:

结果报错:

inputEmailElement = driver.find_element_by_xpath('//div[@class="placeholderContainer"]/input[@name="loginfmt"]')
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 855, in find_element
'value': value})['value']
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="placeholderContainer"]/input[@name="loginfmt"]"}
(Session info: chrome=62.0.3202.94)
(Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.13.1 x86_64)

首先确认的是:

此处的xpath没有写错。

但是就不知道为何始终找不到元素。

selenium NoSuchElementException

selenium selenium.common.exceptions.NoSuchElementException Unable to locate element

webdriver – Unable to locate element throwing a org.openqa.selenium.NoSuchElementException in TestNG – Software Quality Assurance & Testing Stack Exchange

此处也加了wait until了,问题依旧。

java – What is the best way to avoid NoSuchElementException in Selenium? – Stack Overflow

webdriver – org.openqa.selenium.NoSuchElementException: no such element – Stack Overflow

org.openqa.selenium.NoSuchElementException: Unable to locate element – Stack Overflow

Java Code Example org.openqa.selenium.NoSuchElementException

Unable to locate element using selenium webdriver in python – Stack Overflow

Unable to locate element in Python Selenium – Stack Overflow

【总结】

最后是:

driver.refresh()
inputEmailElement = driver.find_element_by_xpath('//div[@class="placeholderContainer"]/input[@name="loginfmt"]’)

或:

inputEmailElement = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//div[@class="placeholderContainer"]/input[@name="loginfmt"]')))

好像都可以。

注:

不过还是不知道为何前面的代码:

# placeholderElement = driver.find_element_by_xpath('//div[@class="phholder"]/div[@class="placeholder"]')
# placeholderElement = WebDriverWait(driver, 10).until(
#     EC.presence_of_element_located((By.XPATH, '//div[@class="phholder"]/div[@class="placeholder"]')))
# placeholderElement = driver.find_element_by_xpath('//div[@class="phholder"]')
# phholderElement = driver.find_element_by_class_name('phholder')
# logging.info("phholderElement=%s", phholderElement)
# placeholderElement = phholderElement.find_element_by_class_name("placeholder")

确保xpath写的是对的,查看页面元素是存在的,但却还是找不到元素。

转载请注明:在路上 » 【已解决】Selenium再次出错:NoSuchElementException: Message: no such element: Unable to locate element

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.173 seconds, using 22.12MB memory