2017-01-10 3 views
0

Mon code doit cliquer sur la valeur dans le menu déroulant, j'ai ce code:Comment écrire mouseover avec 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(); 

J'ai essayé de réécrire votre code, et je vous écris avec 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(); 

Mon erreur est:

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

quelqu'un peut me aider à écrire. Je suis un débutant.

Répondre

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); 

Celui-ci est avec JavaScript

+0

moi: Impossible de résoudre le symbole 'GlobalVars' –

+0

C'était juste mon instance locale de WebDriver. Utilisez celui mis à jour. Devrait marcher. –

+0

Votre code fonctionne, mais ouvre le premier élément dans le menu déroulant, et je veux cliquer sur un autre. –

0

Il y a plus de ces 2 façons de le faire ..

essayer accomplir ce dont vous avez besoin en utilisant ces:

//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; 
        } 

Une autre façon :

//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();