-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.stack.imgur.com/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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    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! – Atomization Jan 15 '20 at 14:50
0

You could do something like:

driver.execute_script("""
  document.querySelector('button').innerText = 'Hi There'
""")
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • 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' """)` – Atomization Jan 14 '20 at 02:13