1

I do not find an elegant solution to address with Selenium an iron-icon.

<iron-icon style="..." icon="vaadin:sign-out" title="logout"></iron-icon>

I made it with Xpath and it works but I would like to have a more elegant solution because Xpath is such thing.

Does anyone know a trick?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
pixelhead
  • 35
  • 8

1 Answers1

0

The <iron-icon> element is from the svg namespace.

To click on the <iron-icon> element you have to induce WebDriverWait for the elementToBeClickable() / element_to_be_clickable() and you can use either of the following locator strategies:

  • Using Java and CSS_SELECTOR:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("iron-icon[icon='vaadin:sign-out']"))).click();
    
  • Using Python and XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg']//*[name()='iron-icon' and @title='logout']"))).click()
    
  • Note : For Python clients you have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions on interacting with SVG elements in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for helping. WebDriverWait ist not furthermore supported that way. Ich changed ist to: `driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));` and then the line with `WebElement logout_button = driver.findElement(By.cssSelector("svg iron-icon[title='logout']"));` It does not work, throws this info: "Unable to locate element: svg iron-icon[title='logout']" and in addition the editor says to that line with findElement "The value of the local variable logout_button is not used". – pixelhead Feb 24 '22 at 12:30
  • @pixelhead Do you realize that you are yet to mention which Language binding art you are using, e.g. Java/Python. Btw, did you find some time to go through the embedded links and discussions? – undetected Selenium Feb 24 '22 at 13:33
  • 1/x Sorry, I am pretty new to this ... I thought I added all information needed. I did my research but probably I simply do not understand or do not find it. Language bindings: Java Stable: 4.1.2 (January 30, 2022) https://www.selenium.dev/downloads/ – pixelhead Feb 24 '22 at 14:56
  • 2/x WebDriverWait: "The constructor WebDriverWait(WebDriver, long) is deprecated" "Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found." / Compiled from WebDriverWait.java (version 1.8 : 52.0, super bit) // Signature: Lorg/openqa/selenium/support/ui/FluentWait; public class org.openqa.selenium.support.ui.WebDriverWait extends org.openqa.selenium.support.ui.FluentWait – pixelhead Feb 24 '22 at 14:56
  • 3/x No idea why this shows up because my imports seem correctly: `import java.time.Duration; 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.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.WebDriverWait;` – pixelhead Feb 24 '22 at 14:57
  • 4/4 it is especially about these two, right? They are in my imports. `import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.WebDriverWait;` – pixelhead Feb 24 '22 at 14:58
  • Check the relevant imports from [this](https://stackoverflow.com/a/45144131/7429447) discussion. – undetected Selenium Feb 24 '22 at 15:15
  • Everything is ok now, it works. Solution: `driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20)); WebElement logout_button = driver.findElement(By.cssSelector("iron-icon[icon='vaadin:sign-out']")); logout_button.click();` – pixelhead Feb 24 '22 at 15:30
  • So you have to say `By.cssSelector("svg iron-icon[title='logout']")` still doesn't works? – undetected Selenium Feb 24 '22 at 15:34
  • In my case it did not work. I also tried without svg and just iron-icon. – pixelhead Feb 24 '22 at 15:41
  • @pixelhead Okay, updated my answer as per the working solution. – undetected Selenium Feb 24 '22 at 15:49