2017-10-20 38 views
0

我正在使用Python和Selenium。我的问题是我无法打开弹出的模式,我无法点击它中的按钮。如何使用Selenium在Python中切换到模态

这是模态的元素:

这是我的代码:

减去当然

browser = webdriver.Chrome(executable_path="D:\\sasdsa\\automate\\chromedriver_win32\\chromedriver.exe") 
user_name = browser.find_element_by_xpath("//input[@id='username']") 
user_name.send_keys("test.employee") 

##Password 
pass_word = browser.find_element_by_xpath("//input[@id='password']") 
pass_word.send_keys("123") 

##log_in = browser.find_element_by_css_selector(".btn") 
log_in = browser.find_element_by_xpath("//button[@class='btn btn-sm btn-primary btn-block']") 
log_in.click() 

##punch 
#driver.find_element_by_id("//@id='product_view') 
#To open the modal 
punch_in = browser.find_element_by_xpath("//button[@class='btn btn-success btn-sm pull-right']") 
punch_in.click() 


#cant switch to the modal to access the button 
browser.switch_to_frame("product_view") 
punch_in2 = browser.find_element_by_xpath("//button[@id='save_me']") 
punch_in2.click() 
+0

你为什么试图切换到'div'作为'iframe'? – Andersson

+0

那么我该怎么办?im试着从主页面切换到模式... –

+0

以及它应该是什么代码? –

回答

0

的URL删除线下,它应该工作精细。

browser.switch_to_frame("product_view") 

你不需要在这里做任何特别的事情。像这样的模态对话框就像HTML页面上的其他HTML一样。你可以像访问其他任何东西一样访问它。

说了这么多......如果你点击一个按钮,等一个启动的对话框中,你可能会添加一个WebDriverWait等待对话框要访问它里面的元素之前可见,等

相关问题