-1

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)

2 Answers2

0

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)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks but my problem is with sending characters using send keys, that's where the code gets stuck as I am sending over 200000 characters – Sarvesh Ahuja Feb 27 '22 at 21:06
  • @SarveshAhuja _sending over 200000 characters_: Sounds to be a different issue/question/requirement all together. Feel free to raise a new question as per your new requirement. – undetected Selenium Feb 27 '22 at 21:08
  • created it here incase you've an answer https://stackoverflow.com/questions/71288911/send-over-200000-characters-using-send-keys-in-selenium – Sarvesh Ahuja Feb 28 '22 at 00:10
  • @SarveshAhuja I'm yet to get your feedback on this question/answer. I'll have a look at your other question at the appropiate time. – undetected Selenium Feb 28 '22 at 12:30
0

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.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Hey! Thanks for improving my code. The problem is with send_keys. The x variable that I am trying to pass has over 200000 characters in different lines which is somewhat crashing the chrome UI. I tried inserting wait time too between click and send keys doesn't help either. – Sarvesh Ahuja Feb 27 '22 at 21:05
  • I'm absolutely not sure it is possible to send `over 200000 characters in different lines` with single `send_keys` action. As a user you will not do that. Selenium imitates real user actions on the GUI elements – Prophet Feb 27 '22 at 21:28
  • @SarveshAhuja you could upvote / accept answers here. I think this improved your code and answered your initial question.... – Prophet Feb 28 '22 at 07:17