Is there a way to edit HTML tags before rendering in the browser using selenium, like when I do driver.Navigate().GoToUrl("www.xyz.com) instead of loading the original HTML, I want to change some href tags?
-
Paste your entire code so that we get an idea where you are stuckLibin Thomas– Libin Thomas2021-07-05 03:57:05 +00:00Commented Jul 5, 2021 at 3:57
-
I don't have any code to demonstrate it. I just want to edit HTML before rendering it in the browser while using selenium.The Acturial Kid– The Acturial Kid2021-07-05 15:01:03 +00:00Commented Jul 5, 2021 at 15:01
-
Don't know why you got Minus on this question, this is a valid one IMHO. I'm stuck with this problem in Python and I found one approach that may help you: Get the webpage content with a simple HTTP request method, modify the elements, pass the the modified content to selenium - see this SO answer for reference: stackoverflow.com/a/36844657/2360229n.r.– n.r.2021-07-15 09:36:27 +00:00Commented Jul 15, 2021 at 9:36
Add a comment
|
1 Answer
To edit any webpage you need to load the html page on your browser first. Sharing a sample patch for reference:
element =driver.find_element_by_id("some-random-number")
#alter innertext value to 200
driver.execute_script("arguments[0].innerText = '200'", element)
Without rendering the source code on the browser you cannot locate the elements to be altered.
Hope it explains you query.
3 Comments
The Acturial Kid
But I am looking to edit it before rendering!! so I can change image tags and several href tags!
Libin Thomas
How are you expecting selenium to identify the xpath/css locator elements of the image and href tags in the webpage without rendering it in the browser?
n.r.
>> Without rendering the source code on the browser you cannot locate the elements to be altered. - depends on how you define rendering, but: sure you can: Just request the plain HTML, parse it to get an adressable DOM, change elements on the fly, pass the DOM to the renderer that finishes the job and creates the final page - got to admit: "rendering" is not the best term in this case