繁体   English   中英

如何使用Java和Selenium WebDriver识别动态WebElement上传图片

[英]How to identify dynamic WebElement to upload the picture using Java and Selenium WebDriver

我正在尝试识别此按钮以单击它并上传图片。 当检查我的 Xpath 时,它会识别它(即使页面被刷新)但在执行代码时 - 没有任何反应。 尝试使用文件路径 SendKeys 或识别此元素的坐标并单击坐标 - 均无效 WebElement -> AddPhoto

    //DISABLE CHROME NOTIFICATIONS
    
                    // Create object of HashMap Class
            Map<String, Object> prefs = new HashMap<String, Object>();
                  
                    // Set the notification setting it will override the default setting
            prefs.put("profile.default_content_setting_values.notifications", 2);
     
                    // Create object of ChromeOption class
            ChromeOptions options = new ChromeOptions();
     
                    // Set the experimental option
            options.setExperimentalOption("prefs", prefs);
     
                    // pass the options object in Chrome driver
    

    System.setProperty("webdriver.chrome.driver", "D:\\Selenium-Facebook\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver(options);   
        driver.get("https://www.facebook.com/marketplace");
        driver.manage().window().maximize();

        
    //ENTER CREDENTIALS
    driver.findElement(By.xpath("//input[@name='email']")).sendKeys("login@email.com");
    driver.findElement(By.xpath("//input[@name='pass']")).sendKeys("password");
    

    driver.findElement(By.xpath("//input[@name='pass']")).sendKeys(Keys.ENTER);     
    
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    
    
    driver.findElement(By.xpath("//a[@href='/marketplace/create/' and  @aria-label='Create New Listing']")).click();
    
    driver.findElement(By.xpath("//a[@href='/marketplace/create/item/']")).click(); 
    
    //Create new listing 
    driver.findElement(By.xpath("//a[@aria-label='Create New Listing']")).click();
    

    //Where the issues begin
    
    //WebElement AddPhoto = driver.findElement(By.xpath("//i[@data-visualcompletion ='css-img' and contains(@style,'u4NKNaueohq.png')]/parent::div/parent::div/parent::div/parent::div/parent::div"));
    
    WebElement AddPhoto = driver.findElement(By.xpath("//input[@accept='image/*,image/heif,image/heic']/parent::*/following-sibling::div[1]/child::div[1]/child::div[1]"));
    //AddPhoto.sendKeys("path to the file");

    
    // Create an object of Actions class and pass reference variable driver as a parameter to its constructor. 
     Actions actions = new Actions(driver); 

// Get the location and Coordinate (x, y) of WebElement. Call getLocation(), getX(), and getY() methods to find the location and coordinate. 
     int getX = AddPhoto.getLocation().getX(); 
    
     int getY = AddPhoto.getLocation().getY(); 

// Call moveByOffset() method of Actions class to move the mouse cursor from initial position to given Offset. 
// Pass the coordinates of x and y as parameters to moveByOffset() method. 
     actions.moveByOffset(getX+1, getY+1).click(); 
     actions.build().perform(); 
  
    

WebElement 试图识别

用于创建选择器的代码

我看到,有2 input field ,类型为文件。

所以你肯定可以这样做来上传文件:-

WebElement AddPhoto = driver.findElement(By.cssSelector("input[type='file']"));
AddPhoto.sendKeys("path to the file");
 //driver.findElement(By.xpath("//a[@aria-label='Create New Listing']")).click();

 The above XPath element was showing the staleElementReferenceException so, handled it with the below explicit wait 
 
new WebDriverWait(driver, 10).until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'Create New Listing')]")))).click();


WebElement AddPhoto = driver.findElement(By.xpath("((//*[@class='mkhogb32'])[1])"));
AddPhoto.sendKeys("File path");


You can check with the above XPath to send the file path

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM