我正在尝试创建一个小的python脚本,但我不断收到错误代码:
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\selenium xpath testing.py", line 30, in <module>
driver.find_element_by_xpath(".//*[@id='page-content']/section[1]/div[1]/nav/div/div[1]/a[1]/i[1]").click()
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":".//*[@id='page-content']/section[1]/div[1]/nav/div/div[1]/a[1]/i[1]"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/Eddie/AppData/Local/Temp/tmpxa66gu8q/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at FirefoxDriver.prototype.findElement (file:///C:/Users/Eddie/AppData/Local/Temp/tmpxa66gu8q/extensions/fxdriver@googlecode.com/components/driver-component.js:10779)
at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/Eddie/AppData/Local/Temp/tmpxa66gu8q/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/Eddie/AppData/Local/Temp/tmpxa66gu8q/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///C:/Users/Eddie/AppData/Local/Temp/tmpxa66gu8q/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\selenium xpath testing.py", line 37, in <module>
except (ElementNotVisibleException or NoSuchElementException) or (ElementNotVisibleException and NoSuchElementException):
NameError: name 'ElementNotVisibleException' is not defined
这是我的代码
count=0
while count < 100:
#this both works
try:
driver.find_element_by_xpath(".//*[@id='page-content']/section[1]/div[2]/div/section/div[2]/div/a[2]").click()
driver.find_element_by_xpath(".//*[@id='page-content']/section[1]/div[1]/nav/div/div[1]/a[1]/i[1]").click()
time.sleep(5)
driver.back()
time.sleep(5)
driver.refresh()
count +=1
print(count)
except (ElementNotVisibleException or NoSuchElementException) or (ElementNotVisibleException and NoSuchElementException):
driver.back()
time.sleep(5)
driver.refresh()
time.sleep(5)
driver.find_element_by_xpath(".//*[@id='page-content']/section[1]/div[2]/div/section/div[2]/div/a[2]").click()
time.sleep(5)
driver.find_element_by_xpath(".//*[@id='page-content']/section[1]/div[1]/nav/div/div[1]/a[1]/i[1]").click()
count +=1
print(count)
为什么当我使用except处理它时,没有定义ElementNotVisible的任何想法?
当我只是将异常更改为异常时:
我还在
Traceback (most recent call last):
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\selenium xpath testing.py", line 44, in <module>
driver.find_element_by_xpath(".//*[@id='page-content']/section[1]/div[1]/nav/div/div[1]/a[1]/i[1]").click()
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 72, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 461, in _execute
return self._parent.execute(command, params)
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Users\Eddie\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///C:/Users/Eddie/AppData/Local/Temp/tmp3pqr2lvm/extensions/fxdriver@googlecode.com/components/command-processor.js:10092)
at DelayedCommand.prototype.checkPreconditions_ (file:///C:/Users/Eddie/AppData/Local/Temp/tmp3pqr2lvm/extensions/fxdriver@googlecode.com/components/command-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/Eddie/AppData/Local/Temp/tmp3pqr2lvm/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/Eddie/AppData/Local/Temp/tmp3pqr2lvm/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///C:/Users/Eddie/AppData/Local/Temp/tmp3pqr2lvm/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)