代码之家  ›  专栏  ›  技术社区  ›  Gaurav Thantry

即使使用JavascriptExecutor滚动也无法单击元素

  •  1
  • Gaurav Thantry  · 技术社区  · 9 年前

    我收到一个WebDriverException,说 Element is not clickable at point (588, 1611)

    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    		int success = 0;
    		driver.get("http://www.gcrit.com/build3/admin");
    		driver.findElement(By.name("username")).sendKeys("admin");
    		driver.findElement(By.name("password")).sendKeys("admin@123");
    		driver.findElement(By.id("tdb1")).click();
    		driver.findElement(By.xpath("//a[text()='Categories/Products']")).click();
    		JavascriptExecutor jse = (JavascriptExecutor)driver;
    		jse.executeScript("window.scrollTo(0,3004)","");
    		
    		String str = "Mockingjay";
    		List <WebElement> lt1 = driver.findElements(By.tagName("td"));
    		Iterator <WebElement> it1 = lt1.iterator();
    	
    		while(it1.hasNext())
    		{
    			
    			WebElement el = it1.next();
    			if(el.getText().contains(str))
    			{
    				WebDriverWait wait = new WebDriverWait(driver, 20);
    				wait.until(ExpectedConditions.elementToBeClickable(el));
    				el.click();
    				break;
    			}
    			
    		}
    		
    		driver.findElement(By.xpath("//span[text()='Edit']")).click();
    		
    		if(driver.findElement(By.xpath("//td[text()='Products Status:']/following-sibling::td/input[1]")).isSelected())
    		{
    			System.out.println("Changing the product status to 'Out of Stock'");
    		}
    		

    这是源代码。我试着把所有的 td tags 在列表中,在其中迭代以查找 element 然后点击它。

     <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1391'">
                    <td class="dataTableContent"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1391&amp;action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="Preview" title=" Preview " /></a> LED TV/52"</td>
                    <td class="dataTableContent" align="center">
    <img src="images/icon_status_green.gif" border="0" alt="Active" title=" Active " width="10" height="10" />  <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&amp;flag=0&amp;pID=1391&amp;cPath="><img src="images/icon_status_red_light.gif" border="0" alt="Set Inactive" title=" Set Inactive " width="10" height="10" /></a></td>
                    <td class="dataTableContent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1391"><img src="images/icon_info.gif" border="0" alt="Info" title=" Info " /></a> </td>
                  </tr>
                  <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1494'">
                    <td class="dataTableContent"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1494&amp;action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="Preview" title=" Preview " /></a> Mock</td>
                    <td class="dataTableContent" align="center">
    <img src="images/icon_status_green.gif" border="0" alt="Active" title=" Active " width="10" height="10" />  <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&amp;flag=0&amp;pID=1494&amp;cPath="><img src="images/icon_status_red_light.gif" border="0" alt="Set Inactive" title=" Set Inactive " width="10" height="10" /></a></td>
                    <td class="dataTableContent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1494"><img src="images/icon_info.gif" border="0" alt="Info" title=" Info " /></a> </td>
                  </tr>
                  <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1507'">
                    <td class="dataTableContent"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1507&amp;action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="Preview" title=" Preview " /></a> Mockingjay</td>
                    <td class="dataTableContent" align="center">
    <img src="images/icon_status_green.gif" border="0" alt="Active" title=" Active " width="10" height="10" />  <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&amp;flag=0&amp;pID=1507&amp;cPath="><img src="images/icon_status_red_light.gif" border="0" alt="Set Inactive" title=" Set Inactive " width="10" height="10" /></a></td>
                    <td class="dataTableContent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1507"><img src="images/icon_info.gif" border="0" alt="Info" title=" Info " /></a> </td>
                  </tr>
    3 回复  |  直到 9 年前
        1
  •  2
  •   JeffC    9 年前

    当我写这样的代码时,我尽量使事情尽可能简单。我将要重用的代码放入函数中,使其更易于调用和维护。

    在您的例子中,您正在遍历所有TDs,以查找包含与您要查找的产品匹配的文本的元素。我们可以使用XPath搜索特定元素,而不是这样做。

    //td[@class='dataTableContent'][contains(.,'Mockingjay')]
    

    Element is not clickable 错误

    public static void deleteSelectedCategory()
    {
        driver.findElement(By.cssSelector("#tdb4 > span.ui-button-text")).click();
    }
    
    public static void editSelectedCategory()
    {
        driver.findElement(By.cssSelector("#tdb3 > span.ui-button-text")).click();
    }
    
    public static boolean isProductInStock(String productName)
    {
        return !driver.findElements(By.xpath("//tr[./td[@class='dataTableContent'][contains(.,'" + productName + "')]]//img[@src='images/icon_status_green.gif']")).isEmpty();
    }
    
    public static void login(String username, String password)
    {
        driver.findElement(By.name("username")).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.id("tdb1")).click();
    }
    
    public static void selectCategory(String categoryName)
    {
        driver.findElement(By.xpath("//a[.='Categories/Products']")).click();
        driver.findElement(By.xpath("//td[@class='dataTableContent'][contains(.,'" + categoryName + "')]")).click();
    }
    

    下面是驱动您的场景的代码

    String productName = "Mockingjay"; // in stock
    // String productName = "Baloon"; // out of stock
    driver.get("http://www.gcrit.com/build3/admin");
    login("admin", "admin@123");
    
    selectCategory(productName);
    boolean inStock = isProductInStock(productName);
    System.out.println("Product is in stock: " + inStock);
    if (inStock)
    {
        editSelectedCategory();
        System.out.println("Changing the product status to 'Out of Stock'");
        // set the product to out of stock
    }
    

    顺便说一句,你不必编辑产品来查看是否缺货。红灯/绿灯指示是否缺货。我编写的代码检查页面上是否指示绿灯,如果指示绿灯,然后编辑产品并添加占位符,您可以在其中添加代码以将产品更改为缺货,等等。

        2
  •  1
  •   yong    9 年前


    List <WebElement> lt1 = driver.findElements(By.tagName("td"));
    

    代码将在页面上找到所有td,而不仅仅是“类别/产品”表中的td。

    列表中的第一个td已经不可点击,其他一些td也不可点击。这就是为什么selenium报告不可点击的错误

    为了解决您的问题,您需要在上述驱动程序中使用更严格的定位器。findElements(),如下图所示: //tbody[tr[@class='dataTableHeadingRow']]//td[@class='dataTableContent'][1]

        3
  •  0
  •   undetected Selenium    9 年前

    Categories/Products ,向下滚动以显示元素 New Category 按钮位于 Viewport 作为参考,创建一个 List xpath WebElement 具有 text() Mockingjay :

    package demo;
    
    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Q46143300_gcreddy_site {
    
        public static void main(String[] args) {
    
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver(); 
            driver.get("http://www.gcrit.com/build3/admin");
            driver.findElement(By.name("username")).sendKeys("admin");
            driver.findElement(By.name("password")).sendKeys("admin@123");
            driver.findElement(By.xpath("//button[@id='tdb1']")).click();
            driver.findElement(By.xpath("//a[text()='Categories/Products']")).click();
            WebElement new_catagory = driver.findElement(By.id("tdb1"));
            JavascriptExecutor jse = (JavascriptExecutor)driver;
            jse.executeScript("arguments[0].scrollIntoView(true);",new_catagory);
            String str = "Mockingjay";
            List <WebElement> my_list = driver.findElements(By.xpath(".//*[@id='contentText']//td[@class='dataTableContent']"));
            for (WebElement item:my_list)
            {
                if(item.getAttribute("innerHTML").contains(str))
                {
                    item.click();
                    break;
                }
    
            }
        }
    
    }