【问题标题】:I need to give link from variable to execute_script(new window) in selenium python我需要在 selenium python 中提供从变量到 execute_script(新窗口)的链接
【发布时间】:2020-10-27 11:47:27
【问题描述】:

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

【问题讨论】:

    标签: python selenium-chromedriver execute-script


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多