El texto de xPath contiene el controlador web Selenium

Oct 27 2020

Estoy intentando seleccionar un elemento en función de su contenido de texto. Estoy usando XPath para lograr esto.

Estoy perplejo, ¿cómo debería funcionar?

WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]")); 

Incluso copiaré el código HTML:

<div class="linkWrap noCount">Notifications&nbsp;<span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>

El elemento div tiene las palabras "Notificaciones" dentro de él. Entonces, ¿por qué no funciona?

Vaya a esta página en Facebook: https://www.facebook.com/settings

Utilice esta extensión de Chrome para resaltar cualquier área a través de xPath.

Respuestas

DMart Oct 27 2020 at 18:12

Tienes un espacio antes de la palabra Notificaciones:

WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));    

También debe agregar un elemento de espera antes de intentar encontrar el elemento:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(), 'Notifications')]"));

WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]")); 
JackTrowbridge Oct 29 2020 at 20:21

Encontré el problema con la ayuda de algunas personas increíbles en esta comunidad.

Ok, entonces mi elemento estaba en un iFrame.

Para acceder al elemento, primero debo acceder al iFrame

WebElement iframe = obj.driver.findElement(By.xpath("//iframe[@tabindex='-1']"));
    
obj.driver.switchTo().frame(iframe);