Home>

I am creating a browser automatic operation program with Selenium.
I want to process only a part of the program automatically during browser operation.

For example, until the ticket selection on the airline site
It is assumed that I want to operate the browser yourself but want to automate the subsequent payment procedure with a program.

In such a case, is there a good way to let the program control the window being operated?

I'm concerned that only browsers launched on the program can be processed by the program ...

If the above concerns are positive, as an alternative
I am happy even if I can behave as follows.

1. Run the program and launch the browser
2. Allow the user to interact while running in some way
3. The program continues with some input

Please point out if there is a lack of explanation.

  • Answer # 1

    How about trying switch_to_window?

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import pprint
    driver = webdriver.Chrome ('pathtodriver')
    driver.get ('https://www.yahoo.com')
    time.sleep (1)
    driver.find_element_by_name ('p'). send_keys ('Selenium Python3')
    driver.find_element_by_name ('p'). send_keys (Keys.ENTER)
    time.sleep (1)
    driver.find_element_by_css_selector ('# web>ol>li: nth-child (1)>div>div>h3>a'). click ()
    pprint.pprint (driver.title)
    driver.switch_to.window (driver.window_handles [1]) # go next window
    pprint.pprint (driver.title)
    driver.switch_to.window (driver.window_handles [0]) # go back window
    pprint.pprint (driver.title)

  • Answer # 2

    Another method is to use pyautogui.

      

    Reference link: Let PyAutoGui do repeated work with Python

    Although it feels unreasonable, I like the mouse and keyboard itself, so I like it intuitively.
    There is also an image recognition function, so you can specify the position of the button.

    import pyautogui as pa