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.
– Alfredo Jamil
@Alfredojamil if not found is because it does not exist or because it is inside an IFRAME.
– Guilherme Nascimento
Perfect William. He was in another iframe. Thank you very much.
– Alfredo Jamil
@Alfredojamil if you can mark the answer as correct
– Guilherme Nascimento