Find widget from code

Asked

Viewed 99 times

-1

I have a table with several lines and in a column x I have a href containing what I need to click, but in this href only a gif is placed and so I can not capture by Partiallinktext.

Is there any way I can find the element from the source code?

Ex of the href link: <a href="conteudo.cfm?ano=2019&amp;mes=8&amp;pid=3974&amp;mobi=1924817">

Since I would like to look for the element by looking for the piece: ano=2019&mes=8

1 answer

2


You can use the selector element[atributo*=valor], he will search anywhere, example:

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;

...

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(getDelay));

var elemento = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a[href*='ano=2019&mes=8']")));

...

If you want to pass "more dynamic" values do:

var ano = "2019";
var mes = "8";

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(getDelay));

var seletor = String.Format("a[href*='ano={0}&mes={1}']", ano, mes);

var elemento = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector(seletor)));

Note that I used WebDriverWait, which is called explicit waiting, it waits the time until the element exists, if it finds before it does not need to wait all the time, it is much better than Thread.Sleep to the specific case

I also used ElementIsVisible, because the condition is that it has to be visible, but if you look for other states of element, follow the list (documentation https://selenium.dev/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm):

  • ExpectedConditions.ElementIsVisible(By)

    Wait to see if an element is present in the DOM of a page and visible. Visibility means that the element is not only displayed, but also has a height and width greater than 0.

  • ExpectedConditions.ElementSelectionStateToBe(By, Boolean)

    Wait to verify that the provided element is in the correct state.

  • ExpectedConditions.ElementSelectionStateToBe(IWebElement, Boolean)

    Wait to verify that the provided element is in the correct state.

  • ExpectedConditions.ElementToBeClickable(By)

    Wait to verify an element is visible and activated so you can click on it.

  • ExpectedConditions.ElementToBeClickable (IWebElement)

    Wait to verify an element is visible and activated so you can click on it.

  • ExpectedConditions.ElementToBeSelected(By)

    Wait to verify that the specified element is selected.

  • ExpectedConditions.ElementToBeSelected(IWebElement)

    Wait to verify that the specified element is selected.

  • ExpectedConditions.ElementToBeSelected(IWebElement, Boolean)

    Wait to verify that the specified element is selected.

  • William, he’s dropping by timeout because he can’t find the element. I tried to change the syntax by putting * (asterisk) at the end after = (equals) and still did not find the element.

  • @Alfredojamil if not found is because it does not exist or because it is inside an IFRAME.

  • Perfect William. He was in another iframe. Thank you very much.

  • @Alfredojamil if you can mark the answer as correct

Browser other questions tagged

You are not signed in. Login or sign up in order to post.