0

I have written a small python script with selenium to search Google and open the first link but whenever I run this script, it opens a console and open a new Chrome window and run this script in that Chrome window.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pyautogui

def main():
    setup()

# open Chrome and open Google
def setup():
    driver = webdriver.Chrome(r'C:\\python_programs'+
                              '(Starting_out_python)'+
                              '\\chromedriver.exe')
    driver.get('https://www.google.com')
    assert 'Google' in driver.title
    mySearch(driver)

#Search keyword

def mySearch(driver):
        search = driver.find_element_by_id("lst-ib")
        search.clear()
        search.send_keys("Beautiful Islam")
        search.send_keys(Keys.RETURN)
        first_link(driver)

#click first link

def first_link(driver):
        link = driver.find_elements_by_class_name("r")
        link1 = link[0]
        link1.click()
main()

How can I open this in the same browser I am using?

  • Do you mean that you want your script to open tab in browser instance that you've opened manually? – Andersson Oct 19 '17 at 08:34
  • It is impossible to use Selenium to open a new tab on the same browser you are using. – Buaban Oct 19 '17 at 08:36
  • Okay. Because I also want that from here, I should be able to pass in a search and once the first link is opened, I copy some contents, check my open tabs if Facebook for example is opened or open it, then update my status with what I copied from the previous website. –  Oct 19 '17 at 09:03
  • You could always send the CTRL+T shortcut directly to the link instead of doing a click action and then handling the new opened window. See https://stackoverflow.com/questions/39281806/python-opening-multiple-tabs-using-selenium – BoboDarph Oct 19 '17 at 10:23
  • @BoboDarph thanks very much. It's cool that way. –  Oct 19 '17 at 12:43
  • If the answer below is helpful and answers your question, you can always mark it as your accepted answer as it will help other people trying to do the same thing you did. And give me 10 Reputation Points that I can use to basically do nothing but they are fun to gather. – BoboDarph Oct 19 '17 at 14:17

2 Answers2

1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

def main():
    setup()

# open Chrome and open Google
def setup():
    driver = webdriver.Chrome()
    driver.get('https://www.google.com')
    assert 'Google' in driver.title
    mySearch(driver)

#Search keyword

def mySearch(driver):
        search = driver.find_element_by_id("lst-ib")
        search.clear()
        search.send_keys("test")
        search.send_keys(Keys.RETURN)
        first_link(driver)

#click first link

def first_link(driver):
        link = driver.find_elements_by_xpath("//a[@href]")
        # uncomment to see each href of the found links
        # for i in link:
        #     print(i.get_attribute("href"))
        first_link = link[0]
        url = first_link.get_attribute("href")
        driver.execute_script("window.open('about:blank', 'tab2');")
        driver.switch_to.window("tab2")
        driver.get(url)
        # Do something else with this new tab now
main()

A few observation: the first link you get might not be the first link you want. In my case, the first link is the login to Google account. So you might want to do some more validation on it until you open it, like check it's href property, check it's text to see if it matches something etc.

Another observation is that there are easier ways of crawling google search results and using googles API directly or a thirdparty implementation like this: https://pypi.python.org/pypi/google or https://pypi.python.org/pypi/google-search

BoboDarph
  • 2,751
  • 1
  • 10
  • 15
0

To my knowledge, there's no way to attach Selenium to an already-running browser.

More to the point, why do you want to do that? The only thing I can think of is if you're trying to set up something with the browser manually, and then having Selenium do things to it from that manually-set-up state. If you want your tests to run as consistently as possible, you shouldn't be relying on a human setting up the browser in a particular way; the script should do this itself.

Claire Nielsen
  • 1,881
  • 1
  • 14
  • 31
  • I can think of a few reasons why someone would want to do that. How about a script to open new tab and automatically log into a webmail account. – Mike C. Dec 14 '18 at 14:33