我需要在 selenium python 中将 link 从变量传递给 execute_script(new window)
I need to give link from variable to execute_script(new window) in selenium python
我有一个代码可以打开一个新的 window 并像代码一样切换到之前和之后。但是我为什么要从变量中给出 link,因为如果我要搜索多个 link。例如: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')")
。
我有一个代码可以打开一个新的 window 并像代码一样切换到之前和之后。但是我为什么要从变量中给出 link,因为如果我要搜索多个 link。例如: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')")
。