无法使用selenium webdriver

时间:2017-03-26 22:04:36

标签: selenium selenium-webdriver

enter image description here无法使用selenium webdriver单击内联元素。

这是网址 https://www.google.com/.

除了图像链接(右侧顶部),还有一个方形图标。需要单击该图标并选择“地图”。 附上截图。 我使用了xpath,cssselector,ID,Name,但没有任何工作。 任何人都可以帮我这个。

代码:

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class webelements2 {
    public static void main(String[] args) throws InterruptedException 
    {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        driver.findElement(By.id("gbwa")).click();
        driver.findElement(By.className("gb_3")).click();               
    }       
}

5 个答案:

答案 0 :(得分:0)

此代码在某种程度上无法在Firefox中运行,因为它会在您单击元素 - By.id("gbwa")时打开“产品”页面。但是,如果您在Chrome中尝试相同,它可以正常工作。您需要更改的内容是By.className("gb_3") By.xpath("//ul[@class='gb_ka gb_da']/li[3]")

WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbwa")).click();
driver.findElement(By.xpath("//ul[@class='gb_ka gb_da']/li[3]")).click();

对于Firefox,您可以修改代码,以便在打开产品页面时,可以单击“地图”选项。

答案 1 :(得分:0)

需要为控件添加一些动态等待。此外,我注意到,如果谷歌没有设置为您的主页,那么您会看到一条消息'经常来这里?将Google设为您的主页。'需要处理此消息,否则将无法访问应用程序图标。

{{1}}

如果您有进一步的疑问,请尝试使用此代码并告诉我们。

答案 2 :(得分:0)

我已经测试了下面的代码,但它确实有用。

driver.get("https://www.google.com");
driver.findElement(By.cssSelector("a[title='Google apps']")).click();
new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.id("gb8"))).click();

您基本上需要点击应用图标,等待地图图标可点击,然后点击它。

我假设您不为Google工作,所以在这种情况下,为什么要测试用户界面?您可以直接导航到https://maps.google.com并跳过用户界面点击。

答案 3 :(得分:0)

这对我有用。

    driver.get("https://www.google.com/");

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='gbwa']//a[@title='Google apps']"))));
    driver.findElement(By.xpath(".//*[@id='gbwa']//a[@title='Google apps']")).click();

    //click map icon
    driver.findElement(By.cssselector("a#gb8")).click();

答案 4 :(得分:0)

使用它,为我工作。

使用Chrome驱动程序代替Firefox驱动程序。

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GoogleMaps {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//*[@id='gbwa']")).click();
        driver.findElement(By.xpath("//li/a[@id='gb8']")).click();
        Thread.sleep(6000);
        driver.quit();
    }
}

输出:

enter image description here

相关问题