我正在运行Ubuntu 14.04,Firefox 49.0.2,Python 3.4.3&硒3.0.1
我想使用Selenium自动化一些浏览器功能,而不是进行任何网站测试。如何修改下面的简单登录脚本以使用桌面上运行的Firefox实例而不是打开新的Firefox窗口?
# login_yahoo_mail.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get('http://mail.yahoo.com/?.intl=us')
enter_email = driver.find_element_by_id('login-username')
enter_email.clear()
enter_email.send_keys('cleanman2@yahoo.com')
next_button = driver.find_element_by_id('login-signin')
next_button.click()
enter_password =
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,' login-passwd'))) enter_password.send_keys(' dumba $$雅虎^&安培;!') signin_button = driver.find_element_by_id(' login-signin') signin_button.click()
谢谢,吉姆
答案 0 :(得分:0)
“Selenium 4”是可能的。
每个窗口都有一个唯一标识符,它在单个会话中保持不变。您可以使用以下方法获取当前窗口的窗口句柄:
driver.current_window_handle
driver = webdriver.Firefox()
# Store the ID of the original window
original_window = driver.current_window_handle
# Opens a new tab and switches to new tab
driver.switch_to.new_window('tab')
# Opens a new window and switches to new window
driver.switch_to.new_window('window')
#Close the tab or window
driver.close()
#Switch back to the old tab or window
driver.switch_to.window(original_window)
我还没有尝试过,但由于 Webdriver 不知道操作系统的焦点在哪里,您可以通过这些选项找到您想要的方式。