如何在Selenium中单击带有锚标记的图像或图标

问题描述 投票:1回答:2

我正在为gmail登录和注销编写脚本。我已成功登录。现在要做Logout,我必须先点击其中有Logout按钮的User图标。我正在编写如下代码,但它不起作用:

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

请让我知道解决方案。提前致谢!

java css selenium xpath selenium-webdriver
2个回答
0
投票

您可以尝试“a [title = Google Account:”+ firstName + lastName +“(”+ eMail +“)]”

firstName,lastName和eMail是String变量。你可能应该使用@title,但我不太确定。

Look here a simple example of concatenation


0
投票

要在用户图标上使用click(),然后在带有文本的链接上使用click()注销,您必须为带有文本的链接引导WebDriverwait,因为注销是可点击的,您可以使用以下代码行:

  • 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();
© www.soinside.com 2019 - 2024. All rights reserved.