2017-07-17 59 views
0

如何在點擊元素後使用硒查找當前網址。我有這樣的網站:http://www.runningintheusa.com/Classic/View.aspx?RaceID=5622單擊鏈接後查找URL

我的代碼(假設所有相關庫進口)

def get_detail(x): 
    dic = {} 
    driver = webdriver.PhantomJS(path) 
    driver.get(x) 
    driver.find_element_by_id('ctl00_ctl00_MainContent_hypPrimaryURL').click() 
    return driver.current_url 
print get_detail('http://www.runningintheusa.com/Classic/View.aspx?RaceID=5622') 

我跑的代碼,它只能回到原來的網址是http://www.runningintheusa.com/Classic/View.aspx?RaceID=5622

哪有我點擊網站上的競賽網站鏈接後找到網址http://flagstaffbigs.org/dave-mckay-run.htm

+0

的可能的複製[硒等到文檔準備就緒(https://stackoverflow.com/questions/15122864/selenium-wait-until-document-is-ready) – Anthon

+0

@Anthon我試圖把時間。睡覺(2)但它沒有工作 –

+0

爲什麼2秒就夠了? – Anthon

回答

2

是因爲一個新標籤打開,這將選擇活動的最新標籤。

driver.switch_to_window(driver.window_handles[-1]) 
return driver.current_url; 
+0

它的工作,但它花了45秒返回的網址,有沒有什麼辦法來縮短進程? –

1

我在這裏試過網站,當你點擊元素時你實際打開了另一個選項卡。因此,driver.current_url會返回原始網址,因爲它沒有更改,您只是使用新網址創建了一個新選項卡。

您需要做的是將驅動程序更改爲新選項卡並獲取其URL,或將鏈接更改爲在同一選項卡中打開。

Here is an example of switching to a new tab in Java.

要改變在同一標籤頁中打開鏈接,你可以簡單地從HTML移除目標=「_空白」。