.send_keys in selenium is messing up indentation when using with pyperclip

三世轮回 提交于 2021-02-11 12:56:55

问题


I'm copying some code from Leetcode to paste in Github, I use pyperclip to paste and copy into clipboard using Selenium. Everything is saved as a string but when i use driver.send_keys(pyperclip.paste()) this is what happens

I've verified that the issue is not Leetcode or Github by copying from Leetcode on my own and then pasting it to Github. The issue is pyperclip because when I paste this is the format

However the issue clearly gets amplified when I use driver.send_keys() because as you can see from the first image that the indentation increases way too much

Here's my code

from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from time import sleep
from bs4 import BeautifulSoup
from urllib.request import urlopen, Request
import pyperclip


driver = webdriver.Chrome()
driver.get('https://github.com/login?return_to=%2Fjoin')

github_username = driver.find_element_by_xpath(
    '//*[@id="login_field"]')
github_username.send_keys('')

github_password = driver.find_element_by_xpath(
    '//*[@id="password"]')
github_password.send_keys('')

github_login = driver.find_element_by_xpath(
    '//*[@id="login"]/form/div[4]/input[9]')
github_login.click()

sleep(3)
url = "https://github.com/user/Leetcode"
driver.get(url)

sleep(3)

leetcode = 'https://leetcode.com/accounts/login/'

driver.execute_script('''window.open("{}","_blank");''')
driver.switch_to.window(driver.window_handles[1])
driver.get(leetcode)

sleep(3)


# driver.switch_to.window(driver.window_handles[0])

leetcode_username = driver.find_element_by_xpath('//*[@id="id_login"]')
leetcode_username.send_keys('')

leetcode_password = driver.find_element_by_xpath('//*[@id="id_password"]')
leetcode_password.send_keys('')

leetcode_login = driver.find_element_by_xpath('//*[@id="signin_btn"]')
leetcode_login.click()

# click on problems
sleep(3)
problems = driver.find_element_by_xpath(
    '//*[@id="lc_navbar"]/div/div[2]/ul[1]/li[3]/a')
problems.click()

sleep(3)
order = driver.find_element_by_xpath(
    '//*[@id="question-app"]/div/div[2]/div[2]/div[2]/table/thead/tr/th[1]')
order.click()
order.click()

sleep(3)
rows = driver.find_element_by_xpath(
    '//*[@id="question-app"]/div/div[2]/div[2]/div[2]/table/tbody[2]/tr/td/span[1]/select')
rows.click()

all = driver.find_element_by_xpath(
    '//*[@id="question-app"]/div/div[2]/div[2]/div[2]/table/tbody[2]/tr/td/span[1]/select/option[4]')
all.click()

for i in range(6, 68):
    current_link = driver.find_element_by_xpath(
        '//*[@id="question-app"]/div/div[2]/div[2]/div[2]/table/tbody[1]/tr[6]/td[3]/div/a')
    current_link.click()
    sleep(3)
    submission = driver.find_element_by_xpath(
        '//*[@id="app"]/div/div[2]/div[1]/div/div[1]/div/div[1]/div[1]/div/div[1]/div/div[4]/a')
    submission.click()
    sleep(3)
    accepted = driver.find_element_by_xpath(
        '//*[@id="app"]/div/div[2]/div[1]/div/div[1]/div/div[1]/div[1]/div/div[5]/div/div/div/div/div/div/div/div/div/table/tbody/tr/td[2]/a')
    accepted.click()
    driver.switch_to.window(driver.window_handles[2])
    sleep(3)
    code = driver.find_element_by_xpath('//*[@id="ace"]/div/div[3]/div/div[3]')

    pyperclip.copy(code.text)

    driver.switch_to_window(driver.window_handles[0])
    driver.get(driver.current_url)
    sleep(3)
    add_file = driver.find_element_by_xpath(
        '//*[@id="js-repo-pjax-container"]/div[2]/div/div[2]/div[1]/div[2]/details/summary')
    add_file.click()
    create_new_file = driver.find_element_by_xpath(
        '//*[@id="js-repo-pjax-container"]/div[2]/div/div[2]/div[1]/div[2]/details/ul/li[3]/form/button')
    create_new_file.click()
    driver.get(driver.current_url)
    body = driver.find_element_by_xpath(
        '//*[@id="js-repo-pjax-container"]/div[2]/div/div/form[2]/div[5]/div[2]/div/div[5]/div[1]/div/div/div/div[5]')
    print(pyperclip.paste())
    body.send_keys(pyperclip.paste())

回答1:


To paste the copied code you can use below JavaScript code and execute with the help of JavaScriptExecutor.

document.querySelector(".CodeMirror").CodeMirror.doc.children[0].lines[0].text = code

Note : Here code the string containing the code. in code editor the code will be shown in single line but when you preview this or save you can see the proper formatted code.



来源:https://stackoverflow.com/questions/63748070/send-keys-in-selenium-is-messing-up-indentation-when-using-with-pyperclip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!