Home > Software engineering >  Send_Keys in selenium (python) is taking longer than expected
Send_Keys in selenium (python) is taking longer than expected

Time:02-28

I am trying to create an HTML table from excel sheet and copy it to a webpage. I am using Send_Keys to sned the characters to 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)

CodePudding user response:

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)
    

CodePudding user response:

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.

  • Related