2017-01-10 2 views
0

Мой код должен нажать на значение из выпадающего меню, у меня есть этот код:Как написать MouseOver с PageFactory

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 

Может кто-то помочь мне, как писать. Я новичок.

ответ

0
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

+0

Выводит это на me: Не удается разрешить символ «GlobalVars» –

+0

Это был только мой локальный экземпляр WebDriver. Используйте обновленный. Должно сработать. –

+0

Ваш код работает, но открывает первый пункт в раскрывающемся меню, и я хочу нажать на другой. –

0

Есть более чем эти 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(); 
Смежные вопросы