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)