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
此处也加了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