2017-11-08 14 views
-1

私はちょうどセレンで始まり、小さな問題に遭遇しています。 基本的に、クリックしたときに新しいタブでリンクをクリックする必要があるウェブページがあります。私はそのタブでいくつかの作業を行い、そのタブを閉じて元のウェブページ上の別のリンクをクリックしたいと思います。セレンでタブを操作する方法(Pythonで)

followinが私のコードです:

w.get("https:\link1")

opens the given link w.find_element_by_class_name("an_element").click()

clicks an element with the given class name which opens in a new tab

w.find_element_by_class_name("another_element")

clicks an element on the opened tab

w.close()

closes the opened tab

w.find_element_by_class_name("an_element").click()

clicks a new element on the previous tab

But I get the following error: w.get("https:\link1")

opens the given link w.find_element_by_class_name("an_element").click()

clicks an element with the given class name which opens in a new tab

w.find_element_by_class_name("another_element")

clicks an element on the opened tab

w.close()

closes the opened tab

w.find_element_by_class_name("an_element").click()

clicks a new element on the previous tab

しかし、私は次のエラーを取得する:

メッセージ:いいえ、そのようなセッション はどのように新しいタブウィンドウ上のいくつかの要素をクリックしていることを保証する、それを閉じることができます私は元の窓を閉じないのですか?

+0

コードをコードとしてフォーマットしてください。書式設定のヘルプが案内します。 – JeffC

+0

作業を終えたら、新しく開いたウィンドウを閉じる必要があります。私が窓を閉めると、元の窓にはアクセスできない。元のウィンドウにアクセスする方法に関するヘルプ? –

+0

投稿したリンクを読んでください。おかげさまで – JeffC

答えて

1
# driver = w 
driver.find_element_by_class_name("new window link").click() 

# open a new tab but your driver is still pointing to original tab.So, 

new_driver_handel = driver.window_handles[-1] 
# -1 indicate last tab 
driver.switch_to.window(new_driver_handel) 
### 
now perform all the activities you should do in new tab using driver 
### 
# at last close the driver 
driver.close() 

# Now again you have to point back to original tab 
main_driver_handel = driver.window_handles[-1] 
driver.switch_to.window(main_driver_handel) 

# By doing this also restores all the session which was there in original tab 
+0

あともう一つだけ 。これはポップアップウィンドウでも使えますか? –

関連する問題