【问题标题】:Not able to click the Inline element using selenium webdriver无法使用 selenium webdriver 单击内联元素
【发布时间】:2023-03-10 13:07:01
【问题描述】:

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

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

除了图片链接(右上角),还有一个方形图标。需要单击该图标并选择地图。 附上截图。 我使用了 xpath、cssselector、ID、名称,但没有任何效果。 谁能帮我解决这个问题。

代码:

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

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

    此代码在 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,您可以修改代码,以便在打开产品页面时,您可以从那里单击地图选项。

    【讨论】:

    • 嗨,Anish,感谢您的回复。再次页面仅导航到产品页面。它不是点击地图图标。
    【解决方案2】:

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

    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;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    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();
            WebDriverWait wait = new WebDriverWait(driver, 10);
            if (driver.findElements(By.xpath("//a[@title='No thanks']")).size() !=0) {
                wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@title='No thanks']"))));
                driver.findElement(By.xpath("//a[@title='No thanks']")).click();
            }
    
            driver.findElement(By.xpath("//div[@id='gb']//div[@id='gbwa']/div/a[@title='Google apps' and @role='button']")).click();
    
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@id='gb8']/span[text()='Maps']"))));
            driver.findElement(By.xpath("//a[@id='gb8']/span[text()='Maps']")).click();               
        }       
    }
    

    如果您有其他疑问,请尝试此代码并告诉我。

    【讨论】:

    • 谢谢马希。 Google 在我的 Firefox 浏览器中设置为默认值。当我使用你的代码时,它要求初始化 WAIT。
    • wait 已在代码中初始化。您是否添加了所需的导入语句?
    • 代码对我有用。可能我们需要检查所有依赖项是否已解决。
    • 我遇到了这个问题“无法从等待类型对非静态方法进行静态引用直到(函数)”
    • wait 中的所有字母都应为小写,即使用 wait 而不是 Wait。如果您遇到进一步的问题,请告诉我。
    【解决方案3】:

    我已经测试了下面的代码,它可以工作。

    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 工作,所以在这种情况下,为什么要测试 UI?您可以直接导航到 https://maps.google.com 并跳过 UI 点击。

    【讨论】:

    • 嗨,杰夫,感谢您的回复。再次页面仅导航到产品页面。它不是点击地图图标。
    • 在您的问题中,它指出,“除了图片链接(右上角)还有一个方形图标。需要单击该图标并选择地图。”如果不是点击地图图标,这意味着什么?
    【解决方案4】:

    这个怎么样……对我有用。

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

    【讨论】:

      【解决方案5】:

      使用这个,为我工作。

      使用 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();
          }
      }
      

      输出:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-01
        • 1970-01-01
        • 2020-11-10
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        相关资源
        最近更新 更多