如何在Selenium中单击出现在小弹窗中的选项?

2024-09-29 21:51:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用selenium制作脚本,将文件从本地磁盘上载到google驱动器。成功登录并点击google drive桌面版上的new按钮之前我是成功的,但之后我无法选择出现在new小窗口下的选项(按下new按钮后出现的窗口)

我的代码到现在为止:

#!/usr/bin/python
    from selenium import webdriver
    import time
    from selenium.webdriver.common.keys import Keys

    browser=webdriver.Firefox()
    #gdURL='https://drive.google.com'
    gdURL='https://accounts.google.com/ServiceLogin?service=wise&passive=true&   continue=http%3A%2F%2Fdrive.google.com%2F%3Futm_source%3Den_US&  utm_medium=button&utm_campaign=web&utm_content=gotodrive&usp=gtd&ltmpl=drive'
   browser.get(gdURL)
   def idIn(email):
       gId=browser.find_element_by_id('identifierId')
       gId.send_keys(email)
       gId.send_keys(Keys.ENTER)
       time.sleep( 10 )
   def passIn(passwd):
       gPass=browser.find_element_by_name('password')
       gPass.send_keys(passwd)
       gPass.send_keys(Keys.ENTER)
       time.sleep( 30 )
   if browser.find_element_by_id('identifierId'):
       idIn('myemail')
       passIn('mypassword')
   #if browser.find_element_by_name('password'):
   #   passIn('mypassword')
   btn=browser.find_elements_by_tag_name('button')
   btn[4].click()

Tags: importbrowsersendnewbytimeseleniumgoogle
1条回答
网友
1楼 · 发布于 2024-09-29 21:51:14

这可能会有所帮助-因此您需要使用窗口句柄切换到新的浏览器实例。我不知道这是否只适用于Windows系统。你知道吗

抱歉,下面的代码是Java语言:

// Store the current window handle 
String winHandleBefore = driver.getWindowHandle(); 
// Perform the click operation that opens new window 
// Switch to new window opened 
for(String winHandle : driver.getWindowHandles()){ 
driver.switchTo().window(winHandle); } 
// Perform the actions on new window 
// Close the new window, if that window no more required driver.close(); 
// Switch back to original browser (first window) 
driver.switchTo().window(winHandleBefore); 
//continue with original browser (first window)

关于这个答案,请参阅源文章here。你知道吗

相关问题 更多 >

    热门问题