简体   繁体   English

无法使用Selenium WebDriver单击“浏览”按钮

[英]Unable to click on the “Browse” button using selenium webdriver

I need to click on the "Browse" button on the below webpage. 我需要点击下面网页上的“浏览”按钮。

http://www.guru99.com/freelancing.html http://www.guru99.com/freelancing.html

I have written the below code but webdriver fails to find the Browse button element. 我已经编写了以下代码,但Webdriver无法找到“浏览”按钮元素。 Please help. 请帮忙。

import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FileUpload {

    public static void main(String[] args) throws IOException {
        System.setProperty("webdriver.gecko.driver", "C:\\Eclipse\\Drivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("http://www.guru99.com/freelancing.html");
        driver.findElement(By.id("theFile_link(Resume)")).click();
        //Below line execute the AutoIT script
        Runtime.getRuntime().exec(System.getProperty("user.dir")+"\\FileUpload.exe");
        driver.close();
    }
}

I'm Using: 我正在使用:

Firefox Version: 49.0.1 Firefox版本: 49.0.1

Selenium Version: Version 3.0.0-beta4 硒版本:版本3.0.0-beta4

OS: Win10 64 bit 操作系统: Win10 64 bit

Java: 1.8 爪哇: 1.8

The form (and Browse button) is inside <iframe> , you need to switch to it first 表单(和“浏览”按钮)位于<iframe>内部,您需要先切换到该表单

WebElement iframe = driver.findElement(By.cssSelector("[src*='recruit'"])); //locate the iframe
driver.switchTo().frame(iframe);

And to switch back 并切换回

driver.switchTo().defaultContent();

use the below code to click on browse: 使用以下代码单击浏览:

//first switch to iframe as the browse button is inside the iframe.
WebElement iframe = driver.findElement(By.cssSelector("[src*='recruit'"])); 
driver.switchTo().frame(iframe);

//scroll into the browse button
WebElement element = driver.findElement(By.id("theFile_link(Resume)"));
((JavascriptExecutor)   driver).executeScript("arguments[0].scrollIntoView(true);", element);

//now click on browse button
element.click();

hope it will help you. 希望对您有帮助。

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

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