我需要在selenium-python中提供从变量到execute_script(新窗口)的链接



我有一个代码,它将打开一个新窗口,并像代码中一样切换到之前和之后。但我为什么要从变量中给出链接,因为如果我有多个链接要搜索。例如:search_url =["https://www.google.com", "https://www.yahoo.com", "https://www.bing.com"]这是我的代码:

driver.get("http://www.facebook.com")
window_before = driver.window_handles[0]
window_before_title = driver.title
print(window_before_title)
#open a new window
driver.execute_script("window.open('http://www.twitter.com', 'new window')")
window_after = driver.window_handles[1]
#switch on to new child window
driver.switch_to.window(window_after)
time.sleep(10)
window_after_title = driver.title
print(window_after_title)
#Compare and verify that main window and child window title don't match
if window_before_title != window_after_title:
print('Context switched to Twitter, so the title did not match')
else:
print('Control did not switch to new window')
#switch back to original window
driver.switch_to.window(window_before)
#Verify that the title now match
if window_before_title == driver.title:
print('Context returned to parent window. Title now match')
print(driver.title)

从这里driver.execute_script("window.open('http://www.twitter.com', 'new window')")我想使用search_url而不是'http://www.twitter.com'

如果我正确理解你写的内容,而不是driver.execute_script("window.open('http://www.twitter.com', 'new window')"),那么你必须在for循环中写driver.execute_script("window.open('" + search_url[i] + "', 'new window')")

相关内容

  • 没有找到相关文章

最新更新