0

I've written a script to test a process involving data input & several pages, but after writing it I've found the forms & main content to be generated from javascript. The following is a snippet of the script I wrote, and after that initial link the content is generated by JS (its my first python script so excuse any mistakes);

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time

browser = webdriver.Firefox()
browser.get('http://127.0.0.1:46727/?ajax=1')
assert "Home" in browser.title
# Find and click the Employer Database link
empDatabaseLink = browser.find_element_by_link_text('Employer Database')
click = ActionChains(browser).click(on_element = empDatabaseLink)
click.perform()
# Content loaded by the above link is generated by the JS
# Find and click the Add Employer button
addEmployerButton = browser.find_element_by_id('Add Employer')
addEmployer = ActionChains(browser).click(on_element = addEmployerButton)
addEmployer.perform()
browser.save_screenshot(r'images\Add_Employer_Form.png')
# Input Employer name
employerName = browser.find_element_by_id('name')
employerName.send_keys("Selenium")
browser.save_screenshot(r'images\Entered_Employer_Name.png')
# Move to next
nextButton = broswer.find_element_by_name('button_next')
moveForward = ActionChains(browser).click(on_element = nextButton)
# Move through various steps
# Then
# Move to Finish
moveForward = ActionChains(browser).click(on_element = nextButton)

How do you access page elements that aren't in the source? I've been looking around & found GetEval but not found anything that I can use :/

4
  • A few pointers, you should be able to just use addEmployerButton.click() rather than the more complex (and some would argue unnecessary) ActionChains approach. Your code also doesn't appear to need Keys or that Exception imported. Also, if you can find a way other than getting the element by text, it would be better; identifying elements by their text content is generally the most brittle way of doing it. Commented Mar 1, 2012 at 14:30
  • Thanks, a few of those imports were left from the examples I was running through this morning to get to grips with all this. And I would love to avoid the locate by text but unfortunately the product wasn't developed with any of this in mind so for now the link has no id or name to select it. I need to get some things changed if possible once I've got a working concept. Commented Mar 1, 2012 at 14:39
  • You should be able to use XPath, CSS, or DOM to locate any distinct entity on the page, with or without a unique ID. Worst case is starting from body and working down, but there should be a quicker way. Also, as to your question, have you tried the quick and dirty approach of seeing how selenium IDE identifies your JS elements? Commented Mar 1, 2012 at 14:53
  • XPath is my new best friend! Thank You @sr2222 - I've just used XPath to locate the link and then the addEmployerButton which I couldn't get to. Commented Mar 1, 2012 at 15:35

1 Answer 1

1

Well, to the people of the future, our above conversation appears to have lead to the conclusion that xpath is what mark was looking for. So remember to try xpath, and to use the Selenium IDE and Firebug to locate particularly obstinate page elements.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah thanks again. I didn't get time to finish the code off today - I was going to answer my own question :D So tomorrow I need to finish off looking up the XPath syntax as I had trouble with a line for the link. xpath(//a[contains(text(),'Employer Database')]). The buttons and fields were easy though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.