How to Open a Link Embeded in a Webelement with in the Main Tab, in a New Tab of the Same Window Using Control + Click of Selenium Webdriver

How to open a link embeded in a webelement with in the main tab, in a new tab of the same window using Control + Click of Selenium Webdriver

As there is a link embedded within in the webelement in the Parent Tab, to open the link in a New Tab in the same window using Selenium and Python you can use the following solution:

To demonstrate the workflow the url https://www.google.com/ was opened in the Parent Tab and then open in new tab functionalty is implemented through ActionChains methods key_down(), click() and key_up() methods.

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    import time

    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.google.com/")
    link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
    ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
  • Note: You need to replace (By.LINK_TEXT, "Gmail") with your desired locator e.g. ("div[data-res-position = '1']")

  • Browser Snapshot:

tab_actions

You can find a relevant Java based solution in Opening a new tab using Ctrl + click combination in Selenium Webdriver


Update

To shift Selenium's focus to the newly opened tab you can find a detailed discussion in Open web in new tab Selenium + Python

Fetch all href link using selenium in python

Well, you have to simply loop through the list:

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
print(elem.get_attribute("href"))

find_elements_by_* returns a list of elements (note the spelling of 'elements'). Loop through the list, take each element and fetch the required attribute value you want from it (in this case href).

WebScraping JavaScript-Rendered Content using Selenium in Python

To scrape the JavaScript-Rendered Content using Selenium you need to:

  • Induce WebDriverWait for the desired element to be clickable().

  • Induce WebDriverWait for the visibility of all elements located().

  • Open each link in a new tab using Ctrl and click() through ActionChains

  • Induce WebDriverWait and switch to the new tab to webscrape.

  • Switch back to the main page.

  • Code Block:

      from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    import time

    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("http://www.txsmartbuy.com/sp")
    windows_before = driver.current_window_handle
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='agency-name-filter' and @name='agency-name']"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='agency-name-filter' and @name='agency-name']//option[contains(., 'Health & Human Services Commission - 529')]"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='spBtnSearch']/i[@class='icon-search']"))).click()
    for link in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//table/tbody//tr/td/strong/a"))):
    ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
    WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
    windows_after = driver.window_handles
    new_window = [x for x in windows_after if x != windows_before][0]
    driver.switch_to_window(new_window)
    time.sleep(3)
    print("Focus on the newly opened tab and here you can scrape the page")
    driver.close()
    driver.switch_to_window(windows_before)
    driver.quit()
  • Console Output:

      Focus on the newly opened tab and here you can scrape the page
    Focus on the newly opened tab and here you can scrape the page
    Focus on the newly opened tab and here you can scrape the page
    .
    .
  • Browser Snapshot:

scrape



References

You can find a couple of relevant detailed discussions in:

  • How to open multiple hrefs within a webtable to scrape through selenium
  • StaleElementReferenceException even after adding the wait while collecting the data from the wikipedia using web-scraping
  • Unable to access the remaining elements by xpaths in a loop after accessing the first element- Webscraping Selenium Python
  • How to open each product within a website in a new tab for scraping using Selenium through Python

Select iframe using Python + Selenium

What finally worked for me was:

        sel.run_script("$('#upload_file_frame').contents().find('img[alt=\"Humana\"]').click();")

Basically, don't use selenium to find the link in the iframe and click on it; use jQuery. Selenium has the capability to run an arbitrary piece of javascript apparently (this is python-selenium, I am guessing the original selenium command is runScript or something), and once I can use jQuery I can do something like this: Selecting a form which is in an iframe using jQuery

Selenum2: Getting same window handle on hyper link click

The context of the window doesn't change if a new window is opened. The same handle is returned by the method driver.getWindowHandle() even if a new window is opened. To interact with the newly opened window, you explicitly have to set the context to the targeted window with driver.switchTo().window(handle);.

This example sets the context on a new window so the driver can interact with it:

WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 20);

// open the url
driver.get("http://stackoverflow.com/");

// open a link in a new window with Ctrl + Click
new Actions(driver)
.keyDown(Keys.CONTROL)
.click(driver.findElement(By.id("nav-questions")))
.perform();

// wait for 2 windows
wait.until(ExpectedConditions.numberOfWindowsToBe(2));

// set the context on the new window
Set<String> handles = driver.getWindowHandles();
handles.remove(driver.getWindowHandle());
driver.switchTo().window(handles.iterator().next());

// display the title of the new window
System.out.println("Page title: " + driver.getTitle());

// quit
driver.quit();

Note that trying to get the last window by getting the last handle of the set is not reliable:

The Get Window Handles command returns a list of window handles for every open top-level browsing context. The order in which the window handles are returned is arbitrary.

https://www.w3.org/TR/webdriver/#get-window-handles



Related Topics



Leave a reply



Submit