2016-12-05 3 views
2

リンクをクリックした後に開く新しいウィンドウに切り替える必要があります。私はウィンドウ名もリンクからのターゲットも持っていないので、move_to.window()は例外をスローしています。どうすれば新しいウィンドウに切り替えることができますか?Selenium/Python:ウィンドウ名を指定しないで新しいウィンドウに移動するにはどうすればよいですか?

編集:私はdriver.switch_to.window()を使用して、ウィンドウハンドルリストの最後のインデックスを取得しています。セレンは、他のウィンドウをつかんで、ドロップダウンからオプションを選択している最初のアクションを実行しているようです。ただし、項目を選択した直後に、私がJUSTでアクションを実行した要素を見つけることができないことを示すNoSuchElementExceptionがスローされます。

driver.find_element_by_xpath("//a[@href=\"link_that_opens_other_window\"]").click() 

    Select(driver.find_element_by_id("programID")).select_by_value("621") 

    driver.find_element_by_xpath("//a[contains(., \"New Schedule Wizard\")]").click() 

    config.long_rest() # short_rest() and long_rest() are custom functions I defined to wait for pages to load 

    driver.switch_to.window(driver.window_handles[-1]) 

    config.short_rest() 

    # In newly opened window, import old rotations 
    Select(driver.find_element_by_xpath("//select[@name = \"rotationsetID\"]")).select_by_value("16") 
    Select(driver.find_element_by_xpath("//select[@name = \"scheduleID\"]")).select_by_index(1) 
    driver.find_element_by_xpath("//input[@value=\"Next Step >\"]").click() 

そして、私は取得していますエラーは次のとおりです:ここに私のPythonコードがある

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@name = "rotationsetID"]"} 
(Session info: chrome=54.0.2840.99) 
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.1.7601 SP1 x86_64) 

私はまた、フレームとして新しいウィンドウを処理しようとしたが、それはどちらかの仕事をしていません。

編集:私が試した中で最も最近のコード:

driver.find_element_by_xpath("//a[@href=\"link_that_opens_other_window\"]").click() 

    Select(driver.find_element_by_id("programID")).select_by_value("621") 

    driver.find_element_by_xpath("//a[contains(., \"New Schedule Wizard\")]").click() 

    config.long_rest() # short_rest() and long_rest() are just custom functions I defined to wait for pages to load 

    old_window = driver.window_handles[0] 
    new_window = driver.window_handles[-1] 

    print "Old: ", type(old_window), old_window 
    print "New: ", type(new_window), new_window 

    driver.switch_to.window(new_window) 

    print "Current window: ", driver.current_window_handle # this line prints out the same value as new_window 

    config.short_rest() 

    # In newly opened window, import old rotations 
    Select(driver.find_element_by_name("rotationsetID")).select_by_value("16") 
    Select(driver.find_element_by_xpath("//select[@name = \"scheduleID\"]")).select_by_index(1) 
    driver.find_element_by_xpath("//input[@value=\"Next Step >\"]").click() 

編集:ここで私はつかむしようとしている要素を持つHTMLコードです:

<table cellspacing="8" cellpadding="0" border="0"> 
    <tbody> 
    <tr> 
     <td>Academic Year: </td> 
     <td> 
     <select name="rotationsetID" onchange="selectAcYearSchedule(this);"> 
      <option value="0">(select academic year)</option> 
      <option value="13">July 1, 2013 - June 30, 2014</option> 
      <option value="14">July 1, 2014 - June 30, 2015</option> 
      <option value="15">July 1, 2015 - June 30, 2016</option> 
      <option value="16">July 1, 2016 - June 30, 2017</option> 
      <option value="17">July 1, 2017 - June 30, 2018</option> 
     </select> 
     </td> 
    </tr> 
    <tr></tr> 
    <tr></tr> 
    </tbody> 
</table> 

編集(2016年9月12日) :まだこれに固執しています。

私は古いウィンドウと新しいウィンドウの両方のウィンドウハンドルをつかむことができ、driver.titleを使用して新しいウィンドウのタイトルを得ることができました。それが開かれた後、新しいウィンドウのタイトルを知っているとすれば、そのタイトルをmove_to.window(new_window_title_here)に渡すと動作すると思います。しかし、私がその関数を呼び出すと、SeleniumはNoSuchWindowExceptionをスローします。 switch_to.window(new_window_handle_here)を呼び出すと、私のprintステートメントは私が新しいウィンドウに切り替わっていることを確認しますが、まだ何か要素を取得することはできません。私はxpath、名前、そしてCSSセレクタを使ってゼロキーを取り戻そうとしました。これはSeleniumやPythonバインディングのバグでしょうか?

+0

'find_element_by_id'を使用するときに' find_element_by_name'を使用していますか? –

+0

@DavidCullen私が探している要素のIDはありません。私は "// select [@name = \" rotationsetID \ "]"などのxpathクエリを使用することを好みますが、名前を使って試しました。 – Ben

+0

私が尋ねたのは、 'find_element_by_id'を使う' programID'の問い合わせがあるからです。だから、 'scheduleID'が実際に' name'の代わりに 'id'だったのか疑問に思っていました。 –

答えて

1

driverのインスタンスには、window_handlesという名前のlistが必要です。あなたはdriver.switch_to_windowを使用して、現在、目的のウィンドウハンドルを保存し、それらを切り替えることができる必要があります:あなただけのリンクをクリックした後、新しいタブを開いた場合

main_window = driver.window_handles[0] 
new_window = driver.window_handles[1] 
driver.switch_to_window(new_window) 
# Do something 
driver.switch_to_window(main_window) 
+0

こんにちはデイビッド、これは私にとってはうまくいかなかった。 @Lucasへの私の返答と私の編集を参照してください。 – Ben

+0

'switch_to.window'の代わりに' switch_to_window'を実行するとどうなりますか?あなたは 'driver.window_handles'を見ることができるようにデバッガでこれを実行しましたか? –

+0

'switch_to_window'は' switch_to.window'の非推奨バージョンです –

0

、それwindow_handlesリストの最後の1でなければなりませんその理論的にあなたができる:

driver.switch_to.window(driver.window_handles[-1]) 

より良い練習には、リンクを開いて、一つのリンクを開いた後に来る新しいものを選ぶ前に、最初のウィンドウハンドルを追跡することです。

+0

これは私にとってはうまくいかなかった。あなたが示唆したように、driver.window_handles [0]とdriver.window_handles [-1]をそれぞれoldとnewとして保存しようとしました。私の編集を見ると、新しいウィンドウに作用するコードの最初の行が実際には機能するドロップダウンから項目を選択するとも言いましたが、直後に私が直前に操作した要素でNoSuchElementExceptionが発生しました。 – Ben

+0

私は混乱しています。あなたの例では、ドロップダウンを2度打つことはありませんが、初めて動作すると言いました。 –

+0

最新の編集を参照してください。 – Ben

関連する問題