myLink = myDriver.find_element_by_xpath ('....')
myLink.click()
bmyLink = WebDriverWait(myDriver, 10).until(ExConditions.presence_of_element_located((By.XPATH, '.....')))
我使用selenium
Firefox
驱动程序Python
3.5。我注意到在click()
另一个Firefox
窗口打开后,所以第二次搜索链接是在第一个窗口中完成的,我猜想,不是我希望它在哪里(在点击页面打开的新窗口)。如何使其工作 - 在同一窗口中打开click()
的新页面?或者?
答案 0 :(得分:2)
如果先在click()
之后打开一个新窗口,则需要先切换新窗口然后再找到如下链接: -
myLink = myDriver.find_element_by_xpath ('....')
myLink.click()
currentWindow = myDriver.current_window_handle
#store current window for backup to switch back
for handle in myDriver.window_handles:
if handle != currentWindow:
myDriver.switch_to_window(handle)
#now you can do your stuff in new window
bmyLink = WebDriverWait(myDriver, 10).until(ExConditions.presence_of_element_located((By.XPATH, '.....')))
----
#now close the new window after doing all stuff
myDriver.close()
#after doing all stuff it new window need to switch back on main window
myDriver.switch_to_window(currentWindow)
希望它会帮助你...... :)