2017-09-25 78 views
0

我想遍歷href鏈接並點擊每個鏈接。我的問題是我無法提取href,因爲它不能幫助我,當我點擊它時,我得到一個空白頁面。我應該直接點擊按鈕列表。迭代點擊href鏈接列表

共有21個節點,hrefList包含21個項目。

這裏是例子的html:

<div class="inner25"> 
<a href="https://www.ida.org.il/?categoryId=96318&itemId=236252">כרטיס רופא ></a> 
</div> 

<div class="inner25"> 
<a href="https://www.ida.org.il/?categoryId=96318&itemId=238647">כרטיס רופא ></a> 
</div> 

我的代碼是:

hrefList = driver.find_elements_by_xpath(".//a[contains(text(), 'כרטיס רופא')]") 
for link in hrefList: 
    link.click() 

網站的鏈接是:https://www.ida.org.il/?pageType=19&langId=1&paramIds=%2Con_321%2Con_322%2Con_354%2Con_355%2Con_320&scope=&parameterSearch=

在每次看病廣場。我想點擊它們中的每一個。

我發現該循環打開第一個鏈接,然後失敗。什麼可能是一個問題?

I tried also this code: 
      for href in range(1,3): 
       hrefList[href].click() 

First link it open, and then failed. 
This is an error : 
    File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 78, in click 
    self._execute(Command.CLICK_ELEMENT) 
    File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 499, in _execute 
    return self._parent.execute(command, params) 
    File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute 
    self.error_handler.check_response(response) 
    File "C:\Program Files\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document 
    (Session info: chrome=60.0.3112.113) 
    (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.15063 x86_64) 
+0

如果您可以分享英文和文字的頁面細節,您要點擊,拍攝屏幕截圖並將其置於有問題 – thebadguy

+0

您的xpath(「.// a [contains(text(),'你已經共享了 – thebadguy

+0

你的xpath不正確 – iamsankalp89

回答

0

使用此xapth返回你的網站

//a[contains(@href, 'https://www.ida.org.il/?categoryId=96318&itemId')] 

此代碼Java中的21個節點。你必須回到主頁和定位元素

public static void main(String[] args) { 

String URL="https://www.ida.org.il/?pageType=19&langId=1&paramIds=%2Con_321%2Con_322%2Con_354%2Con_355%2Con_320&scope=&parameterSearch="; 
WebDriver driver=new FirefoxDriver(); 
driver.get(URL); 
List<WebElement> links=driver.findElements(By.xpath("//a[contains(@href, 'https://www.ida.org.il/?categoryId=96318&itemId')]")); 
System.out.println("Total links: "+links.size()); 
for (int i=0;i<links.size();i++) { 
links.get(i).click(); 
System.out.println(i+"Current URL is: "+driver.getCurrentUrl()); 
driver.navigate().back(); 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
links=driver.findElements(By.xpath("//a[contains(@href, 'https://www.ida.org.il/?categoryId=96318&itemId')]")); 
} 
driver.close(); 

} 
+0

對,我知道hrefList的大小是21個節點,這就是我想要的。但是,當我運行循環,它打開第一個鏈接,然後失敗。 –

+0

好吧,我可以給你的代碼在Java中,因爲我不知道太多關於python,你也必須來到同一頁。點擊另一個鏈接 – iamsankalp89

+0

好的,我會很高興。但也許是瀏覽器或類似的問題。我使用鉻。我附加了另一個我嘗試過的代碼,以及我得到的錯誤。請看看。 –

0

初學者問題。你可以找到很多有關陳舊元素異常的問題。

您不應該使用已找到的對象列表來更改循環中的頁面,否則您將獲得陳舊元素,因爲該元素未附加到DOM。

您應該將所有href的值保存在列表中,然後在循環中,您應該根據保存的href值執行find元素,並調用單擊找到的元素。

如果頁面更改/重新加載,您需要再次找到該元素。