0

This question is primarily based on answer of following question Paste command using Selenium

My goal is to paste a text from clipboard into a certain text field in a certain website.

What should i enter for "self" and "text" while calling the function.The function is defined this way

def paste_keys(self ,xpath, text):
    os.system("echo %s| clip" % text.strip())
    el = self.driver.find_element_by_xpath(xpath)
    el.send_keys(Keys.CONTROL, 'v')

If just enter the xpath of the textbox like this

paste_keys('//*[@id="txt-url"]')

, i get an error saying that "two positional arguments are missing in the line xyz".xyz line is the line containing above mentioned code (where i have called the function providing only xpath arguments.)

If simply remove "text and self" arguments from the function i.e

def paste_keys(xpath):
    os.system("echo %s| clip" % text.strip())
    el = self.driver.find_element_by_xpath(xpath)
    el.send_keys(Keys.CONTROL, 'v')

, I get another error saying "text" not defined.

As a last attempt to fix it, I tried to pass a blank argument while calling function i.e

paste_keys( , '//*[@id="txt-url"]' , )

I get invalid syntax error, and arrow pointing at the first comma

Although I could have waited for the reply for the comment, I wish to understand this error and concept in a much deeper sense, as i'm a hobbyist learning python.

MY updated code

from config import data
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time

#driver = webdriver.Chrome()

driver = webdriver.Chrome('./chromedriver')

def paste_keys(xpath):
    #el = driver.find_element_by_xpath(xpath) 
    el = driver.find_element_by_css_selector(xpath)
    el.send_keys(Keys.CONTROL, 'v')


def order(k): 
    
    driver.get(k['downloader_url'])

    driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
    
    paste_keys("input[id='txt-url']" )
    time.sleep(10)
    driver.find_element_by_css_selector("a[data-fquality='480']").click()
    time.sleep(5)
    #driver.find_element_by_link_text('Download .mp4').ActionChains.contextClick(productLink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform()
    
    download_url_element = driver.find_element_by_xpath("//div[@id='process-result']/div/a")
    download_url_element_value=download_url_element.get_attribute("href")
    print(download_url_element_value)
if __name__ == '__main__':
    order(data)


Chemist
  • 126
  • 12
  • You want **us** to decide which parameters **you** want to use and which not? – JaSON Oct 02 '20 at 14:57
  • The thing is I don't understand those parameters mentioned in the linked answer. I just want to paste clipboard text into a textfield – Chemist Oct 02 '20 at 14:59
  • That was an approach for **pasting text from clipboard**. If you want just to paste text use `driver.find_element_by_xpath(xpath).send_keys(TEXT)` – JaSON Oct 02 '20 at 15:01
  • @JaSON even i wish to paste from clipboard – Chemist Oct 02 '20 at 15:01

1 Answers1

1

If your method is inside a class then self makes sense there otherwise not.

self is used to refer to the class object itself.

Let's say you have a class which contains your method as a function of itself

class Hooks:
    def paste_keys(self ,xpath, text):
        os.system("echo %s| clip" % text.strip())
        el = self.driver.find_element_by_xpath(xpath)
        el.send_keys(Keys.CONTROL, 'v')

when you are gonna use that method it will be like this

Hooks hooksObject = Hooks()
hooksObject.paste_keys(xpath, text)

Python will convert your call into - Hooks.paste_keys(hooksObject, xpath, test);

Second, if you are not using the method as a class function then you can rewrite it as

def paste_keys(xpath, text):
   os.system("echo %s| clip" % text.strip())
   el = driver.find_element_by_xpath(xpath) #keep in mind - here driver has to be global, if not - you can pass it as parameter
   el.send_keys(Keys.CONTROL, 'v')

and use it like

paste_keys(xpath, text)

Also there is a gret post about self

Alin Stelian
  • 861
  • 1
  • 6
  • 16
  • its not inside a class in my program, should i paste my complete code to get better understanding? – Chemist Oct 02 '20 at 15:05
  • So basically i can get rid of self parameter. Also what argument should I provide for "text", or how can get rid of "text" – Chemist Oct 02 '20 at 15:07
  • yes - you can get rid of self it is outside a class. The "pasted" text is in a variable? or you want to do copy paste? – Alin Stelian Oct 02 '20 at 15:10
  • I wish to paste from clipboard, the required text is already in the clipboard, it's not present in a variable. And I really apreciate your efforts to help me – Chemist Oct 02 '20 at 15:12
  • os.system("echo %s| clip" % text.strip()) - it retains the text in clipboard – Alin Stelian Oct 02 '20 at 15:15
  • So it takes text from clipboard and puts it back into clipboard? – Chemist Oct 02 '20 at 15:16
  • no man, when you call that method you need to add one xpath and a text variable or text itself like 'paste_keys('elementXPAHT', 'how are you today')' -> the method will retain the 'how are you today' in the clipboard and it will paste it – Alin Stelian Oct 02 '20 at 15:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222411/discussion-between-chemist-and-alin-stelian). – Chemist Oct 02 '20 at 15:21