点击 X 元素以关闭电子邮件订阅弹窗。

huangapple go评论146阅读模式
英文:

How to click on the X element to close the email subscription popup

问题

我试图通过点击X按钮来关闭这个电子邮件订阅弹窗。我尝试使用xpath来选择它,但没有成功。

代码尝试:

driver.findElement(By.xpath("/html/body/div[27]/div/div/div/div/div/div/button/svg/circle")).click();

元素快照:

点击 X 元素以关闭电子邮件订阅弹窗。

任何帮助将不胜感激。

英文:

I'm trying to close this email subscription popup by clicking on the X. I tried using xpath to select it but I've been unsuccessful.

Code trial:

driver.findElement(By.xpath("/html/body/div[27]/div/div/div/div/div/div/button/svg/circle")).click();

Element snapshot:

点击 X 元素以关闭电子邮件订阅弹窗。

Any help would be appreciated.

答案1

得分: 0

在你的情况下,唯一的选择器是 [role=dialog] button[aria-label*=Close]

WebDriverWait wdwait = new WebDriverWait(driver, 10);

driver.get("https://www.marksdailyapple.com/");
wdwait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[role=dialog] button[aria-label*=Close]"))).click();
英文:

In your case unique selector is [role=dialog] button[aria-label*=Close]

<!-- begin snippet: java hide: false console: true babel: false -->

<!-- language: lang-java -->

WebDriverWait wdwait = new WebDriverWait(driver, 10);

driver.get(&quot;https://www.marksdailyapple.com/&quot;);
wdwait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(&quot;[role=dialog] button[aria-label*=Close]&quot;))).click();

<!-- end snippet -->

答案2

得分: 0

以下是您要翻译的内容:

"The element <kbd>X</kbd> within the email subscription popup is a SVG element.


Solution

To click on the svg element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • Using cssSelector:

    driver.get(&quot;https://www.marksdailyapple.com/#axzz3HChquQM8&quot;);
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;button[aria-label=&#39;Close form 3&#39;] &gt; svg &gt; path&quot;))).click();
    
  • Using xpath:

    driver.get(&quot;https://hausrat.allianz.de/&quot;);
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//button[@aria-label=&#39;Close form 3&#39;]//*[name()=&#39;svg&#39;]//*[name()=&#39;path&#39;]&quot;))).click();
    
  • Browser snapshot:

点击 X 元素以关闭电子邮件订阅弹窗。"

英文:

The element <kbd>X</kbd> within the email subscription popup is a SVG element.


Solution

To click on the svg element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • Using cssSelector:

    driver.get(&quot;https://www.marksdailyapple.com/#axzz3HChquQM8&quot;);
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;button[aria-label=&#39;Close form 3&#39;] &gt; svg &gt; path&quot;))).click();
    
  • Using xpath:

    driver.get(&quot;https://hausrat.allianz.de/&quot;);
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//button[@aria-label=&#39;Close form 3&#39;]//*[name()=&#39;svg&#39;]//*[name()=&#39;path&#39;]&quot;))).click();
    
  • Browser snapshot:

点击 X 元素以关闭电子邮件订阅弹窗。

huangapple
  • 本文由 发表于 2023年8月5日 01:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定