最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How do I dump entire dom structure using selenium webdriver - Stack Overflow

matteradmin6PV0评论

I am new at selenium and python, and I am having problems finding the best way to identify the page elements I need for my automation. I have a webpage with a lot of javascript on it. When I use firefox's inspect element for the username field in the login form, I see an input tag with an id, but when I ask selenium to find that id it says it can't be found. I want to double-check that what I saw in firefox is actually what selenium is seeing, so I tried:

with open("login.html","w") s f:
    f.write(driver.page_source)

I see no input elements at all in the resulting file.

Based on another stackoverflow question I tried:

DOM=driver.execute_script("return document.documentElement.outerHTML")
with open("login.html","w") as f:
    f.write(DOM)

Still no input elements.

Is there a better way to see all the dom elements and/or find the correct xpath/ids to sue for my selenium script ?

I am new at selenium and python, and I am having problems finding the best way to identify the page elements I need for my automation. I have a webpage with a lot of javascript on it. When I use firefox's inspect element for the username field in the login form, I see an input tag with an id, but when I ask selenium to find that id it says it can't be found. I want to double-check that what I saw in firefox is actually what selenium is seeing, so I tried:

with open("login.html","w") s f:
    f.write(driver.page_source)

I see no input elements at all in the resulting file.

Based on another stackoverflow question I tried:

DOM=driver.execute_script("return document.documentElement.outerHTML")
with open("login.html","w") as f:
    f.write(DOM)

Still no input elements.

Is there a better way to see all the dom elements and/or find the correct xpath/ids to sue for my selenium script ?

Share Improve this question asked May 18, 2018 at 18:46 dnraikesdnraikes 3351 gold badge5 silver badges17 bronze badges 4
  • Check whether form located inside an iframe – Andersson Commented May 18, 2018 at 19:22
  • @anderson, there are not iframes in teh html as obtained by driver.page_source. – dnraikes Commented May 18, 2018 at 19:26
  • No. Not in page source. Check DOM of page pletely rendered in browser: right click on input field -> Inspect element -> Check ancestors – Andersson Commented May 18, 2018 at 19:28
  • Looking at the page using firefox's inspector, it doesn't look like there is an iframe anywhere in the entire dom. However, I did notice that there are refferences to oraclejet views and models and templates, so I don't know how those get rendered. – dnraikes Commented May 18, 2018 at 22:35
Add a ment  | 

2 Answers 2

Reset to default 3

Try get all body HTML by document.body.innerHTML

html = driver.execute_script("return document.body.innerHTML;")
with open("login.html","w") as f:
    f.write(html)

@yong, your suggestion of adding a long sleep before the execute_script was the right answer. Now I can see the entire html source in the file I created.

In addition now my PageObject code works to fill in the login form and submit it. I do another sleep and then print the pageurl and title to make sure I have moved on to the next page.

Final code:

driver = webdriver..Firefox()
driver.set_page_load_time(60)
driver.get(URL)
time.sleep(60)
print("URL: "+driver.current_url)
print("Title: "driver.title)
page=LoginPage(driver)
page.username="username"
page.password="password"
page.signin_button.click()
time.sleep(60)
print("URL: "+driver.current_url)
print("Title: "+driver.title)
driver.quit()

Thank you everyone for the suggestions.

Post a comment

comment list (0)

  1. No comments so far