【问题标题】:How to write mouseover with PageFactory如何使用 PageFactory 编写鼠标悬停
【发布时间】:2017-01-10 20:52:39
【问题描述】:

我的代码应该点击下拉菜单中的值,我有这个代码:

WebElement element = driver.findElement(By.xpath("//a[text()='Product Category']"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[text()='iMacs']")), 500);
WebElement subElement = driver.findElement(By.xpath("//a[text()='iMacs']"));
action.moveToElement(subElement);
action.click();
action.perform();

我试图重写你的代码,我用 PageFactory 编写:

WebElement element = mouse_over_product_category;
Actions action = new Actions(driver);
action.moveToElement(element).perform();
waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[text()='iMacs']")), 500);
WebElement subElement = link_iMacs;
action.moveToElement(subElement);
action.click();
action.perform();

我的错误是:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

谁能帮我写字。我是初学者。

【问题讨论】:

    标签: selenium drop-down-menu mouseover pageobjects


    【解决方案1】:
    WebElement element=driver.findElement(By.xpath("//a[text()='Product Category']"));        
    String mouseOver = "var evObj = document.createEvent('MouseEvents');" +
                                "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
                                "arguments[0].dispatchEvent(evObj);";
    
    
       ((JavascriptExecutor)driver).executeScript(mouseOver, element);
    

    这个是 JavaScript 的

    【讨论】:

    • 输出给我:无法解析符号'GlobalVars'
    • 这只是我本地的 WebDriver 实例。使用更新的。应该工作。
    • 您的代码有效,但打开下拉菜单中的第一项,我想点击另一个。
    【解决方案2】:

    有不止这两种方法可以做到这一点..

    尝试使用这些来完成您需要的事情:

    //Conventional Way- Identifying the Select Menu Box and sending the text desired to select either by Text or Index or by value
    
    
    
        WebElement elDropDownItem = driver.findElement(By.xpath(""));
    
                    if (elDropDownItem != null) {
                        Select comboBox = new Select(elDropDownItem);
    
    //Either of these 3 should work SelectBy-- should work
                            comboBox.selectByVisibleText(MenuItemText);
                            break;
                //OR        
                            comboBox.selectByIndex(Integer
                                    .parseInt(MenuItemIndex));
                            break;
                //OR        
                            comboBox.selectByValue(MenuItemValue);
                            break;
                        }
    

    另一种方式:

    //Clicking the down arrow and then clicking on the Select Item you desire
                WebElement element= driver.findElement(By.xpath("downArrow"));
                element.click();
    
    WebElement element1= driver.findElement(By.xpath("selectMenuItem"));
                element1.click();
    

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 2011-08-04
      • 2018-09-03
      • 1970-01-01
      • 2017-03-06
      • 2015-08-08
      • 2015-04-22
      • 2011-05-26
      • 2017-10-14
      相关资源
      最近更新 更多