Linktext不起作用

时间:2017-04-09 04:47:30

标签: java selenium selenium-webdriver

以下是我在练习Selenium webdriver时编写的代码。在下面的代码linkText中没有获取所需的值。完成以前有关同一问题的答案中给出的所有内容。

package newpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class PG9 {
    public static void main(String[] args) {
        System.setProperty("webdriver.ie.driver","D:\\Tools & Utilities\\IEDriverServer_x64_3.3.0\\IEDriverServer.exe");
        InternetExplorerDriver driver = new InternetExplorerDriver();
        String baseUrl = "http://www.monsterindia.com";
        //WebDriverWait myWaitVar = new WebDriverWait(driver, 50);
        driver.get(baseUrl);
        //myWaitVar.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.linkText("Upload Resume")));

        driver.findElement(By.linkText("Upload Resume")).click();

        String expectedTitle = "Job Search | Job Vacancies | Job Opportunities in India | Monster India";
        String actualTitle = "";
        actualTitle = driver.getTitle();

        /*
         * compare the actual title of the page with the expected one and print
         * the result as "Passed" or "Failed"
         */
            if (actualTitle.contentEquals(expectedTitle)){
                driver.findElement(By.id("name")).sendKeys("Abhay Dadhkar");
                driver.findElement(By.id("mob_no")).sendKeys("9011134418");
                driver.findElement(By.id("wordresume")).sendKeys("D:\\Abhay\\Abhay_Training_Profile\\Training_Profile\\AbhayDadhkar_Detail_Profile_Jan2017.docx");
            } else
            {
                System.out.println("Test Failed");
            }
    driver.close();

        // enter the file path onto the file-selection input field
        WebElement uploadElement = driver.findElement(By.name("File name"));
        uploadElement.sendKeys("D:\\Automation tools\\Selenium\\Selenium_Practice\\newhtml.html");

        // check the "I accept the terms of service" check box
        //driver.findElement(By.name("Open")).click();

        // click the "UploadFile" button
        driver.findElement(By.name("Open")).click();
        }

}

1 个答案:

答案 0 :(得分:0)

By.linkText仅适用于<a>代码。如果您想通过文字查找,请使用xpath代替

driver.findElement(By.xpath("//span[contains(., 'Upload Resume')]")).click();

您也可以点击父<div>

driver.findElement(By.className("fileUpload")).click();