Skip to content

Instantly share code, notes, and snippets.

@bebef1987
Last active December 16, 2015 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bebef1987/5434882 to your computer and use it in GitHub Desktop.
Save bebef1987/5434882 to your computer and use it in GitHub Desktop.
sel test
<!DOCTYPE html>
<html>
<body>
<ul id="myList">
<li>Coffee
<ul>
<li>element 2</li>
<li>element 3</li>
</ul></li>
<li>Tea <ul>
<li>element 2</li>
<li>element 3</li>
</ul></li>
<li>Milk <ul>
<li>element 2</li>
<li>element 3</li>
</ul></li>
</ul>
<p id="demo">Click the button to remove the first item from the list</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var list=document.getElementById("myList");
list.removeChild(list.childNodes[0]);
list.removeChild(list.childNodes[0]);
}
</script>
</body>
</html>
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://localhost:8000/test.html") # Load page
el = browser.find_element('css selector', '#myList>li')
print el.text
browser.find_element('css selector', 'html>body>button').click()
print el.text
browser.close()
(sel)bebe@P5171:~/test$ python test.py
Coffee
element 2
element 3
Traceback (most recent call last):
File "test.py", line 13, in <module>
print el.text
File "/home/bebe/.virtualenvs/sel/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 50, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "/home/bebe/.virtualenvs/sel/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 228, in _execute
return self._parent.execute(command, params)
File "/home/bebe/.virtualenvs/sel/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "/home/bebe/.virtualenvs/sel/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 152, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: u'Element is no longer attached to the DOM' ; Stacktrace: Method fxdriver.cache.getElementAt threw an error in resource://fxdriver/modules/web_element_cache.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment