'Send_Keys in selenium (python) is taking longer than expected
I am trying to create an HTML table from excel sheet and copy it to a webpage. I am using Send_Keys to send over 200000 characters (indifferent lines) to a webpage but it is causing memory issue and crashing jupyter. My code is as below. I am looking for ways to speed up the process to copy the variable x in my code on the webpage.
sheet_to_df_map = pd.read_excel(r'.xlsx', sheet_name='')
x = sheet_to_df_map.to_html()
x = str(x)
time.sleep(30)
button = driver.find_element_by_id("editPageLink")
button.click()
time.sleep(30)
driver.switch_to.frame(driver.find_element_by_id("wysiwygTextarea_ifr"))
button1 = driver.find_element_by_xpath("//body[@data-id='wysiwygTextarea']//p")
button1.click()
time.sleep (30)
button1.send_keys(x)
Solution 1:[1]
As the element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#wysiwygTextarea_ifr"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//body[@data-id='wysiwygTextarea']//p"))).send_keys(x)
Solution 2:[2]
You are using a hardcoded pauses of 30 seconds 3 times here. All these seems to be possibly reduced with use of Expected Conditions explicit waits.
Please try this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 30)
sheet_to_df_map = pd.read_excel(r'.xlsx', sheet_name='')
x = sheet_to_df_map.to_html()
x = str(x)
wait.until(EC.element_to_be_clickable((By.ID, "editPageLink"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"wysiwygTextarea_ifr")))
button1 = wait.until(EC.element_to_be_clickable((By.XPATH, "//body[@data-id='wysiwygTextarea']//p")))
button1.click()
button1.send_keys(x)
In case you need to put a delay between button1 click and sending a text there you still can put some short delay between the last 2 code lined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | undetected Selenium |
| Solution 2 | Prophet |
