Selenium Basic Python

Asked

Viewed 128 times

0

Greetings, I am beginner in Selenium and follows below my script:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

links=['']
for link in links:

    browser=webdriver.Chrome(r'C:\Users\Administrador\Downloads\chromedriver.exe')
    browser.execute_script("window.open('https://www.onlinevideoconverter.com/pt/youtube-converter');")
    time.sleep(1)

    ak=browser.find_element_by_id('texturl')
    ak.send_keys(link)

    au = WebDriverWait(browser, 30).until( EC.element_to_be_clickable((By.XPATH, '//*[text() = "Começar"]')) )
    au.click()

    af = WebDriverWait(browser, 90).until(
    EC.presence_of_element_located((By.ID, 'downloadq'))
    )

    browser.execute_script('arguments[0].click();', af) 

That basically takes the download link and does it automatically.

The problem that happens is that when I use browser.execute_script("window.open('https://www.onlinevideoconverter.com/pt/youtube-converter');") the method ak=browser.find_element_by_id('texturl') is not loaded. Even using time.sleep.

I wanted to know if it is possible to solve without using the browser.get('https://www.onlinevideoconverter.com/pt/youtube-converter')

What’s wrong?

1 answer

0

The problem is that by using the method window.open() the browser opens a new window, so you have to instruct your webdriver to shift the focus to this new window:

browser = webdriver.Chrome(r'C:\Users\Administrador\Downloads\chromedriver.exe')
browser.execute_script("window.open('https://www.onlinevideoconverter.com/pt/youtube-converter');")
time.sleep(1)

new_window = browser.window_handles[1]
browser.switch_to.window(new_window)

ak = WebDriverWait(browser, 90).until(EC.presence_of_element_located((By.ID, 'texturl')))
ak.send_keys(link)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.