Selenium Python, my copy paste method doesn’t work

  Kiến thức lập trình

I am trying to get the string from text box s and copy paste it into text box b, then return from text box b.

a is the driver, b and s are different text boxes.

def workaround(a, b, s):
    b.clear()
    actions = ActionChains(a)
    actions.click(s)
    actions.key_down(Keys.CONTROL)
    actions.send_keys("a")
    actions.send_keys("c")
    actions.key_up(Keys.CONTROL)
    actions.click(b)
    actions.key_down(Keys.CONTROL)
    actions.send_keys("v")
    actions.key_up(Keys.CONTROL)
    actions.perform()
    
    result = b.get_attribute('value')
    return result

This returns a blank space. If I remove the b.clear() then it returns the original value of b. If I remove actions.key_down(Keys.CONTROL) and actions.key_up(Keys.CONTROL) then ‘v’ is returned.

edit to make reproducible:

def main():
    options = webdriver.ChromeOptions()
    options.add_argument('headless')
    browser = webdriver.Chrome(options=options)
    url = 'https://www.gps-coordinates.net/'
    browser.get(url)
    address = browser.find_element(By.ID, 'address')
    address.clear()
    address.send_keys('this shouldnt show up')
    button = browser.find_element(By.CSS_SELECTOR, '.btn.btn-primary')
    button.click()
    lat = browser.find_element(By.ID,'latitude')
    lat_value = workaround(browser, address, lat)
    print(lat_value)
    return lat_value

New contributor

Julian Brochu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

LEAVE A COMMENT