0

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?

3
  • Paste your entire code so that we get an idea where you are stuck Commented 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. Commented 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/2360229 Commented Jul 15, 2021 at 9:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

But I am looking to edit it before rendering!! so I can change image tags and several href tags!
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?
>> 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.