2016-10-10 137 views
0

我是該網站的新手。我試圖選擇/點擊http://autotask.net上的元素。我曾嘗試使用以下的方法來找到元素:接收selenium.common.exceptions.NoSuchElementException:消息:無法定位元素消息

driver.find_element_by_css_selector( '#HREF_btnPrint> IMG:第n個孩子(1)') driver.find_element_by_xpath( 'browser.find_element_by_xpath('/ HTML /體/形式[1]/DIV [2]/DIV [1]/UL /李[2]/A/IMG')

...但它給我的標題除外。

我能找到元素罰款在登錄頁面(即'用戶名''密碼'字段),但一旦我登錄 - 硒在頁面上找不到任何元素。

我一直在學習/編碼一個fe現在w周,我對Java一無所知。如果您需要更多信息,請讓我知道 - 謝謝。

回答

0

我不確定python的語法,所以我會用Java代替。我認爲有兩種可能的情況,首先你的驅動程序在頁面加載時查看DOM結構。另一個是網絡驅動程序沒有指向DOM的根源。我通常有等待方法等待頁面加載完成,並在查找某些元素之前切換到默認內容。

首先,我通過構造函數傳遞WebDriver,Element id和Element名稱。然後我有waitUntilReady()執行邏輯來檢查特定超時內元素的存在。

public void waitUntilReady() { 
      WebDriverWait waiter = new WebDriverWait(getWebDriver(), 30); 
      waiter.withMessage("\"" + getElementName() + "\" is not ready in 30 seconds") 
        .until(new Predicate<WebDriver>() { 
         @Override 
         public boolean apply(WebDriver input) { 
          /* isReady() returns true if all elements 
          * that you need to test are available. 
          * Note that if you have javascript, you 
          * should have some hidden field to tell 
          * you that the whole page has loaded. */ 
          return isReady(); 
         } 
        }); 
     } 

以及開關默認內容之前找到元素

WebElement getWebElement() { 
    WebElement webElement; 
    try { 
     getWebDriver().switchTo().defaultContent(); 
     webElement = getWebDriver().findElement(By.id(elementId)); 
    } catch (NoSuchElementException ex) { 
     getLogger().log(Level.SEVERE, "Element id '" + elementId + "' does not exist"); 
     throw ex; 
    } 
    return webElement; 
} 

如果它不工作,你可以有<body id="container">,然後用driver.findElementById( 「容器」)。findElementById( 「你的目標元素」)

+0

我對Java並不熟悉。我用得到你正在說的,然後找到元素byid方法。我需要python語法來查找元素中的元素。 – MrGlass