0

我有一个代码,它将打开一个新窗口并切换到之前和之后,就像在代码中一样。但是我为什么要从变量中给出链接,因为如果我有多个要搜索的链接。例如: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'.

4

1 回答 1

1

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

于 2020-07-07T09:03:24.033 回答