I am trying to use python selenium to change the text in a specific JS element.
I started out by attempting to simply execute the script using the following:
driver.execute_script("document.querySelector('#cssSelector').textContent='{}'".format(text))
However, unfortunately, this returns null
When looking into the JS the selector is in a specific iframe so I have tried to select the iframe but this has not yet worked:
driver.execute_script('''var element=window.frames['iframe'].document.querySelector("[id='cssselector']"); element.textContent='{}'".format(text))
This code also returns undefined as it cannot find the element.
Is there a better way to do this? thanks
Related
Problem:
I am trying to find an element and then click it via xpath using selenium and python, on a web page that is rendered with JavaScript. The xpath has been confirmed to be correct, the issue is that the html isn't fully rendering due to only seeing the javascript "top level" (i think that's the right term).
I have seen methods of finding the full html code and saving it as a string, but that is not clickable via selenium
What I Am Looking For:
A method to just render all the javascript code, so that I can access the html elements I am looking for.
Code Would Look Like:
browser.render_javascript()
browser.find_element_by_xpath("//span[text()='Budget & Cost Management']/../preceding-sibling::img[2]").click()
Currently if I try to find the xpath above, it says it doesn't exist. However, if I inspect the page and search this xpath, it takes me right to the element. Obviously, selenium isn't seeing this element though because it isn't available to it (not rendered or executed, not sure which is the correct term).
I have also tried to just wait 30 seconds before searching for this element, but it doesn't look like any amount of time will render the page to selenium.
Is this possible? What kind of workarounds are available?
For a school project, I'm trying to create a Python Script which is able to fill in different forms, from different websites.
Here is the thing, for some kinds of website, I'm not able to catch the form elements which Selenium.
E.g. in this website : http://www.top-office.com/jeu
Where I inspect the page with firefox, the input "Name" box has the id "lastname", but Selenium is not able to get it. And when I display the html code of this page, the form looks like being included from another page or something.
I tried to getelementbyid, byname, bycssselector, etc.
I also tried to wait for the page to be entirely loaded by using WebDriverWait(driver, 5), but it still do not work.
Do you have any solutions or suggestions ?
PS : Even with javascript, I'm not able to get this element by id
Thanks
The element that you are trying to interact with is inside of an iframe. You need to use the driver.switch_to_frame() method in order to switch the driver to focus on that iframe. Only when it has switched into that iframe can it interact with its elements.
See this post on how to use it. You can locate the iframe using xpath, like this: //iframe[#src = 'https://adbx.me/grandjeuclubmed/']. Then, you can use the code mentioned in the post I gave you.
I have an applescript program that executes javascript in Google Chrome's front browser tab.
Whenever my script is run, I need to select the element with data-id play-pause and .click it. I cannot use jQuery as it's not included with the document.
Is there a way in pure javascript that I can do this?
(I'm sure there's probably a way to import it so I could use it but I'm not going down that path for a simple program.)
From IE8 and up you can use querySelector, or querySelectorAll if there's more than one element
document.querySelector('[data-id="play-pause"]').click();
i have the following html output of a tool thats i'm developing:
<button>a</button>
<script>$('what to wrtie here to select the previous button element?').click(function(){alert ('a clicked !');});</script>
<button>b</button>
<script>$('same here').click(function(){alert ('b clicked !');});</script>
i need the selector in each script tag to select the previous element.
using id selector cant be applied since the tool don't generate ids for elements.
thanks.
ANSWER:
$('script').last().prev().click(...
as Niklas said the current executed script is the last one in the list
This is not possible without an id or some other kind of reference to either the button object or the script tag itself.
It's not possible to do because the script is not executed from where its element is located in the DOM. Instead it's executed with reference to the whole window.
Actually, there's a pretty good answer here: How may I reference the script tag that loaded the currently-executing script?
In short, the currently running script is the last element in the list.
well , i really don't recommend what you are doing first lets talk about your approach,
this kind of code should be wrapped in a ready event and when the DOM is ready all the registered code associated with that event will run , so no way to understand what script tag the code were in
what should happen is moving all the script tags to its own file and using selectors to select what elements you want or selecting them dynamicly using prev,next, parents, etc
Edit
i am wrong about not being able to get the script tag #Niklas answer is the right one, but i am still thinking very wrong to do so
There is no way of doing this (referring to the script tag that contains the script) that I know of. The best approach here would be to generate an ID for each element and aggregate your script into a single script tag.
That up there is the correct solution. The secret, naughty solution is this (spoiler):
<button>a</button><script>
//script#0001
$('script:contains("#0001")').prev().click(function(){
alert('foo');
});</script><button>b</button><script>
//script#0002
$('script:contains("#0002")').prev().click(function(){
alert('bar'); });</script>
DON'T USE IT
I'm using Selenium RC to write test cases for a website that's at least partially dynamically generated or otherwise dependent on a database. I'd like to be able to tell my Python program making calls to Selenium to do string operations on the target of a specific link, but I can't be sure what the target (the href='' field) will be at "compile time" (or, well, whatever the Python equivalent is. Can someone help me out there?).
Now, I am sure what the xpath of that link is going to be; "//table[#id='search-results']/tbody/tr[2]/td[3]/a/img" . And I am running Selenium, which has an eval() function (the root of all evil) that runs arbitrary lines of javascript. Is there any way I can get the target of this link from the xpath and Selenium, or do I have to find some way to download the entire page source (how do I do this? There was supposed to be a Selenium command to download the entire page source, but I got an "element doesn't exist" error when I tried running it) and then break out regexes to mess with the entire page source?
I think you can do this using the get_attribute Python method here: http://code.google.com/p/selenium/source/browse/trunk/py/selenium/selenium.py#1343
The call would look something like:
href = mySelenium.get_attribute("//table[#id='search-results']/tbody/tr[2]/td[3]/a#href")
Your XPath expression selects an img element, but it seems to me that you want to get at its parent a element. So if your XPath is correct, then the link target should be:
<...value-of select="//table[#id='search-results']/tbody/tr[2]/td[3]/a#href"/>
or perhaps:
<...for-each select="//table[#id='search-results']/tbody/tr[2]/td[3]/a">
<...value-of select="#href" />
</...for-each>
where ... is your namespace