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

前端 未结 3 2036
一个人的身影
一个人的身影 2020-12-06 21:51

There is a link embedded in a web element in the Main Tab, I want to open that link in a new tab in the same window using Selenium Webdriver and python. Perform some tasks i

3条回答
  •  醉话见心
    2020-12-06 22:16

    This is trick can be achieve if your locator return a correct href.

    Try this first:

    href = driver.find_element_by_css_selector("div[data-res-position = '1']").get_attribute("href")
    print(href)
    

    If you get correct href, you can do:

    #handle current tab
    first_tab = driver.window_handles[0]
    
    #open new tab with specific url
    driver.execute_script("window.open('" +href +"');")
    
    #hadle new tab
    second_tab = driver.window_handles[1]
    
    #switch to second tab
    driver.switch_to.window(second_tab)
    
    #switch to first tab
    driver.switch_to.window(first_tab)
    

    Hope this helps.

提交回复
热议问题