admin管理员组文章数量:1026154
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 aniframe
– 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
2 Answers
Reset to default 3Try 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.
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 aniframe
– 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
2 Answers
Reset to default 3Try 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.
本文标签: javascriptHow do I dump entire dom structure using selenium webdriverStack Overflow
版权声明:本文标题:javascript - How do I dump entire dom structure using selenium webdriver - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745626123a2159871.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论