-2

I want to basically do a temporary edit to a webpage (like how people do it using inspect element) but have it done automatically using selenium. For example this is an image from google.ca :

https://i.sstatic.net/CSXi9.jpg

I simply want to change the text of "Gmail" and "Images" into whatever I want.

I only have this so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome('/Users/--------/Downloads/chromedriver')
browser.get("https://google.ca/")
x = browser.find_element_by_id('Gmail')

Is there a way I can do this?

6
  • 1
    but why though? Commented Jan 13, 2020 at 23:19
  • @SuperStew I just want it so that when the browser is ran, it has my custom text in it. Commented Jan 13, 2020 at 23:24
  • for what purpose though? Commented Jan 13, 2020 at 23:25
  • I don't think you can set attributes with selenium unfortunately. Commented Jan 13, 2020 at 23:30
  • @SuperStew Do you know any libraries that support this by any chance? Commented Jan 13, 2020 at 23:33

2 Answers 2

1

To replace the text of a WebElement within a webpage e.g. the LINK_TEXT Gmail on Google Home Page with a customized text e.g. Atomization you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.google.ca/')
    element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
    driver.execute_script("document.querySelector('div#viewport a').innerText = 'Atomization'")
    
  • Browser Snapshot:

change_innerText

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

1 Comment

So this does work, However, it doesnt change the gmail. it changes the left side of the browser where it says "About" beside Store. I'm just gonna treat this as an answer though. Thanks!
0

You could do something like:

driver.execute_script("""
  document.querySelector('button').innerText = 'Hi There'
""")

1 Comment

Doesn't seem to be working. It doesn't even give an error. browser = webdriver.Chrome('/Users/-------/Downloads/chromedriver') browser.get("https://google.ca/") browser.execute_script(""" document.querySelector('button').innerText = 'Hi There' """)

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.