【问题标题】:How to click on image or icon with anchor tag in Selenium如何在 Selenium 中单击带有锚标记的图像或图标
【发布时间】:2018-02-28 10:31:10
【问题描述】:

我正在编写一个用于 gmail 登录和注销的脚本。 我已经成功登录。 现在要注销,我必须先点击里面有注销按钮的用户图标。 我正在编写如下代码,但它不起作用:

driver.findElement(By.cssSelector("a[title=Google Account: FirstName LastName   (email@gmail.com)]")).click();

请告诉我解决方案。提前致谢!

【问题讨论】:

  • 添加相关的 HTML。

标签: java css selenium xpath selenium-webdriver


【解决方案1】:

您可以尝试类似“a[title=Google Account:” + firstName + lastName + "(" + 电子邮件 + ")]"

firstName、lastName 和 eMail 是字符串变量。您可能应该使用@title,但我不太确定。

Look here a simple example of concatenation

【讨论】:

    【解决方案2】:

    用户图标上的click(),然后到带有文本为退出的链接上的click(),您必须诱导WebDriverwait 让文本为 Sign out 的链接可点击,您可以使用以下代码行:

    • cssSelector

      //Click on the image    
      driver.findElement(By.cssSelector("a[role=button][title^='Google Account']")).click();
      //Click on Sign out
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href^='https://accounts.google.com/Logout']"))).click();
      
    • xpath

      //Click on the image    
      driver.findElement(By.xpath("//a[@role='button' and contains(@title,'Google Account') and contains(@href,'https://accounts.google.com/SignOutOptions')]")).click();
      //Click on Sign out
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(@href,'https://accounts.google.com/Logout')]"))).click();
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-05
      • 2015-07-28
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      相关资源
      最近更新 更多