I'm kinda new to coding and would appreciate any help - i can't get text with selenium from DOM which is generated by JS. I will attach the screen below. I'm trying to get the text like this:
driver.findElement(By.xpath("//div/a[#class='item']")).getText()
But i'm getting such error and couldn't find any answers on stackoverflow and anywhere else:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
Screenshot
Related
Please read the problem and requirement carefully as I have searched and tried innumerable things before adding a new question.
This is the code to get all elements from inspect element. Even though the code is in Python, Javascript code to do it will also work as I am executing it in Python.
from selenium import webdriver
url="https://www.websiteWithLotsOfJavascript.com"
driver = webdriver.PhantomJS(executable_path=r'my_path')
driver.get(url)
#This will get the initial html - before javascript
html1 = driver.page_source
# This will get the html after on-load javascript
html2 = driver.execute_script("return document.documentElement.outerHTML;")
#copied.txt has the manually copied inspect element
f=open("copied.txt",encoding="utf8")
copiedString=f.read();
print(len(copiedString))
print(len(html1))
print(len(html2))
OUTPUT:
3914543
588849
588740
The lengths of html1 and html2 are almost same but length of the copiedString (which is manually copied by me by going into inspect element and then right clicking the outermost HTML tag and then clicking Edit As HTML and then selecting all text and copy) is almost 6 times the length of html1 and html2.
I have tried both document.documentElement.outerHTML and document.documentElement.innerHTML.
I also tried pausing the program with time.sleep() before script execution line (thinking maybe network delay may cause everything to not load) but same result. I guess I have read various articles and almost every stackoverflow question on getting inspect element but nothing seems to work.
What can be causing the difference OR is there a way to get the complete HTML by some other means?
I'm trying to click on href javascript link with Selenium in Python.
The HTML looks like this:
HTML Example
and I want to click on javascript:goType(1).
this is what I tried to do:
advance_search = browser.find_element_by_xpath("//a[#href='javascript:goType(1)']")
advance_search.click()
but it failed with: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a"}
Moreover, when I trying to print all "a" tags it prints an empty lists (Maybe this cause the error). Is There a chance that it isn't possible?
I searched for similars answers but they didn't helped. Plese help me :).
I think I realized something: When I did browser.find_elements_by_tag_name("body") it didn't found anything, but when I
tried with "head" it did found, and then I discovered that there is a 'page source' and a 'frame source', and my code works only on the page source and not on the frame source.
It doesn't finds anything because all my code is in the frame source.
How could I run selenium on the frame source?
I am integrating a live chat functionality and I am trying to use a JQuery statement to get the chat session ID from a div ID that is not created until a chat is started. (It is dynamically added to the page.) Of course logically, I am making sure that I don't try to select the element until after a chat has started and it exists. Here is the relevant HTML structure and the line of code that I am using:
HTML:
<!--These do not exist until a chat is started-->
<div class="customer_inner_widget">
<div id="chat-session-5748220" style="height:100%;">
JQuery:
$(".customer_inner_widget").children().first().attr("id").split("-")[2]
The strangest thing is that under normal circumstances either when I run it in my script upon closing the chat OR I run it in Google Chrome Developer Tools Console AFTER a chat has started, it does not work:
Output:
Uncaught TypeError: Cannot read property 'split' of undefined
at <anonymous>:1:58
...but If I simply right click on the chat window on my page and select "inspect", then run the same line of code in the console... it works:
Output:
"5748220"
How does just inspecting the HTML wilth Chrome suddenly cause it to work...? How can I perform this hidden magic in my script?
So the issue seems to have something to do with the fact that the child I am trying to access is located within an iFrame and apparently you cannot access external dynamically added content within an iFrame. I guess that JQuery just becomes aware of what is inside after inspecting since it is being forced to discover the elements.
I'm trying to click on a webpage item via AppleScript but with no success.
I tried the following code in chrome and safari:
execute "document.getElementById('t-strikethrough').click();"
and
do JavaScript "document.getElementById('t-strikethrough').click();"
In Safari, the first line of code does nothing and the result section of Script Editor says missing value. With Chrome, I get the following error:
Can’t make "document.getElementById('t-strikethrough').click();" into
type specifier
This is included inside tell statements to locate app, window and document.
Without seeing all of your AppleScript code, it's difficult to see exactly what the problem is. You may want to try setting a variable for the URL like this:
set URL of document 1 to "http://www.whatever.com"
do JavaScript "document.getElementById('t-strikethrough').click();" in document 1
Check this post to see the full example . Click Button on Webpage with Applescript/JavaScript
I want to find elements inside an iframe tag. However, in the HTML source there isn't any iframe tag. If I inspect element there is, though. How to solve this using Selenium library in Python 2.7?
HTML source
Screenshot
Inspect element
Screenshot
If it's dynamically generated, it could explain why it's not found in the Selenium version of the DOM. You can still get it by using JavaScript in your code.
driver.execute_script("return document.getElementsByClassName('card-fields-iframe')")