[英]Selenium WebDriver - cssselector for locating input + onclick
[英]Selenium cssSelector works in IDE but not in Webdriver
我有一個使用Java/Selenium Webdriver
自動化的Web系統。 我有一個正在嘗試訪問的項目。 它具有復合類名。 我已經嘗試過在這里能找到的所有解決方案,但到目前為止,這些解決方案都沒有。
提供最多的解決方案如下所示:
By elem = By.cssSelector("div.prdbox.saleshdr");
List<WebElement> elements = driver.findElements(elem);
System.out.println("Number of Items found: "+elements.size());
當我檢查elements數組的大小時,它始終為零。
但是,我發現的是,當我將選擇器字符串放入Selenium IDE (2.9.1)
並使用“查找”按鈕時,它可以毫無問題地識別正確的Web元素。
我不知道為什么它可以在IDE
工作,但不能在我的代碼中工作。
嘗試使用其XPath選擇元素嗎? 過去,當我遇到嘗試使用cssSelector進行選擇的問題時,我嘗試使用XPath時常常會取得成功。
在選擇器之前給一些等待時間。
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> elements = driver.findElements(By.cssSelector("div.prdbox.saleshdr"));
System.out.println("Number of Items found: "+elements.size());
或嘗試借助Xpath或id查找元素。
List<WebElement> elements = driver.findElements(By.xpath("your xpath"));
希望對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.