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?
Asked
Active
Viewed 368 times
0

The Acturial Kid
- 232
- 1
- 10
-
Paste your entire code so that we get an idea where you are stuck – Libin Thomas Jul 05 '21 at 03: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 Jul 05 '21 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: https://stackoverflow.com/a/36844657/2360229 – n.r. Jul 15 '21 at 09:36
1 Answers
0
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.

Libin Thomas
- 829
- 9
- 18
-
But I am looking to edit it before rendering!! so I can change image tags and several href tags! – The Acturial Kid Jul 05 '21 at 18:55
-
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? – Libin Thomas Jul 06 '21 at 05:22
-
>> 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 – n.r. Jul 15 '21 at 09:44