Send keys control + click in Selenium with Python bindings

前端 未结 6 854
甜味超标
甜味超标 2020-12-01 00:54

I need to open link in new tab using Selenium.

So is it possible to perform ctrl+click on element in Selenium to open it in new tab?

相关标签:
6条回答
  • 2020-12-01 01:21

    Below is what i have tried for Selenium WebDriver with Java binding and its working for me. If you want to manually open the Link in New Tab you can achieve this by performing Context Click on the Link and selecting 'Open in new Tab' option. Below is the implementation in Selenium web-driver with Java binding.

    Actions newTab= new Actions(driver);
    WebElement link = driver.findElement(By.xpath("//xpath of the element"));
    
    //Open the link in new window
    newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
    

    Web-driver handles new tab in the same way to that of new window. You will have to switch to new open tab by its window name.

    driver.switchTo().window(windowName);
    

    You can keep track of window-names which will help you to easily navigate between tabs.

    0 讨论(0)
  • 2020-12-01 01:25

    By importing Keys Class, we can open page in new tab or new window with CONTROL or SHIFT and ENTER these keys: driver.find_element_by_xpath('//input[@name="login"]').send_keys(Keys.CONTROL,Keys.ENTER)

    or

    driver.find_element_by_xpath('//input[@name="login"]').send_keys(Keys.SHIFT,Keys.ENTER)

    0 讨论(0)
  • 2020-12-01 01:26

    Following is working for me to open link in new tab :

       String link = Keys.chord(Keys.CONTROL,Keys.ENTER); 
       driver.findElement(By.linkText("yourlinktext")).sendKeys(link);
    

    Above code is in java. you can convert to python easily I assume.

    Please ask if have any query.

    0 讨论(0)
  • 2020-12-01 01:34

    Use an ActionChain with key_down to press the control key, and key_up to release it:

    import time
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome()
    
    driver.get('http://google.com')
    element = driver.find_element_by_link_text('About')
    
    ActionChains(driver) \
        .key_down(Keys.CONTROL) \
        .click(element) \
        .key_up(Keys.CONTROL) \
        .perform()
    
    time.sleep(10) # Pause to allow you to inspect the browser.
    
    driver.quit()
    
    0 讨论(0)
  • 2020-12-01 01:35

    Two possible solutions:

    opening a new tab

    self.driver = webdriver.Firefox()
    self.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 
    

    this is the solution for MAC OSX. In other cases you can use the standard Keys.CONTROL + 't'

    opening a new webdriver

    driver = webdriver.Firefox() #1st window
    second_driver = webdriver.Firefox() #2nd windows 
    
    0 讨论(0)
  • For python, a good solution would be driver.find_element_by_css_selector('<enter css selector here>').send_keys(Keys.CONTROL, Keys.RETURN)

    After that, you should change windows so selenium could function in the new window

    window_before = driver.window_handles[0]    
    window_after = driver.window_handles[1]
    driver.switch_to_window(window_after)
    

    After using the window is useful to close it or go back to the original:

    Close tab: driver.close()

    Back to the original: driver.switch_to_window(window_before)

    Also try driver.switch_to.window() if your Selenium version does not support the other one

    0 讨论(0)
提交回复
热议问题