I am looking for an element
string outerShadow = "return document.querySelector('#printessMainGrid > div.printess-stage > div.printess-content > wc-doc')" +
".shadowRoot.querySelector('div > wc-spread')";
IWebElement outerShadowElement = VU.executeQuerySelector(outerShadow);
FYI - in my library
public IWebElement executeQuerySelector(String query)
{
return (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(query);
}
Is there a way to add timespan like wait for 30 seconds, before the queryselector returns an error? Please advice.
I havent been able to successfully add any timespans. I am not sure of the code syntax it uses.
Related
I have an issue that I am facing.
I have a text in the text box that I want to delete, the problem is that
driver.find_element_by_id('foo').clear()
not work and I need something harder than this clear function that do nothing.
I used this mthood that actually worked in windows:
element.sendKeys(Keys.CONTROL + "a");
element.sendKeys(Keys.DELETE);
if I want it to run on mac and on Linux machine, how can I perform it?
please the clear() not worked please do not provide solution with the clear() method
Use execute_script:
element=driver.find_element_by_id('foo');
driver.execute_script("arguments[0].value=' ';", element);
try
driver.findElement(yourelement).sendKeys("");
or variant
use Actions action = new Actions(driver);
// ... // may be move cursor to field
action.sendKeys(Keys.ARROW_LEFT);
action.build().perform();
may be problem in selenium library error, or in the webdriver version,
or conflict with js-framework on a form?
i use this also
public void clearAndInputStringData(By locator, String text) throws Exception {
ClickElementWhenClickable(locator);
WebElement element = getWebDriver().findElement(locator);
// element.sendKeys(Keys.CONTROL + "a");
Actions actions = new Actions(getWebDriver());
actions.doubleClick(element).perform();
element.sendKeys(Keys.DELETE);
element.sendKeys(text);
}
I am trying to automate - verification of top nav links of https://www.zillow.com.
For that I am matching actual urls with expected urls. But in page class method this line retruns null. I don't get it why?
String urlp = locator.all_topnav_links.get(i).getAttribute("href");
Method in page class:
public void verify_topnav_links() {
System.out.println("Started");
for (int i = 0; i < locator.all_topnav_links.size(); i++) {
System.out.println(locator.all_topnav_links.size());
if (locator.all_topnav_links.get(i).isDisplayed() == true) {
String link=locator.all_topnav_links.get(i).getText();
System.out.println(link);
String urlp = locator.all_topnav_links.get(i).getAttribute("href");
System.out.println("Links are : " + urlp);
// now click link one by one
locator.all_topnav_links.get(i).click();
driver.navigate().back();
// 2 verify before url with current opened url
//Assert.assertEquals(urlp, driver.getCurrentUrl());
}
Locator in locator class:
#FindAll({#FindBy(xpath = "//ul[#data-zg-section='main']/li")})
public List<WebElement> all_topnav_links;
Test case in test class:
#Test
public void verify_TopNav_links()
{
hpage.verify_topnav_links();
}
Also I am not sure whether this correct approach or not. Can anybody tell me the best approach to verify this scenario.
Note: This zillow.com will starts to display captcha if we open multiple times using selenium. To ignore it manually remove catpcha parameters from the url when test case is getting executed.
It appears you need to use
//ul[#data-zg-section='main']/li/a
here
#FindAll({#FindBy(xpath = "//ul[#data-zg-section='main']/li")})
public List<WebElement> all_topnav_links;
This xpath
//ul[#data-zg-section='main']/li
Seems to contain multiple elements. But you're trying to access the href attribute directly. The li here doesn't have a href attribute.
I am trying to perform an explicit wait in Katalon (which uses Groovy). I have the following code :
// wait on page change to "Dashboard"
WebDriverWait dashboardChangeWait = new WebDriverWait(driver, 3)
/* This is causing the following Exception :
* - GroovyCastException : Attempt to cast 'true' with class 'java.lang.Boolean' to class
* 'org.openqa.selenium.WebElement'
* */
WebElement element = dashboardChangeWait.until(
ExpectedConditions.textToBe(By.cssSelector('.breadcrumb-item.active'), "DASHBOARD"))
which is giving me a GroovyCastException. I know that WebDriverWait.until takes a Function (yay, JavaScript-like coding!) argument, and that ExpectedConditions.textToBe returns a ExpectedCondition<Boolean> and until's signature is V org.openqa.selenium.support.ui.FluentWait.until(Function<Object, Object<V>> arg0) . Is there a way to perform this type of wait in Katalon, that avoids this issue?
You were pretty close. The ExpectedConditions method textToBe() is defined as follows:
public static ExpectedCondition<java.lang.Boolean> textToBe(By locator, java.lang.String value)
An expectation for checking WebElement with given locator has specific text
Parameters:
locator - used to find the element
value - used as expected text
Returns:
Boolean true when element has text value equal to #value
So you just need to change the return type to boolean instead of WebElement as follows:
Boolean status = dashboardChangeWait.until(ExpectedConditions.textToBe(By.cssSelector('.breadcrumb-item.active'), "DASHBOARD"))
I am using Selenium WebDriver and its was working all fine and today i am getting either timeout if i use the below code or getting the error Unable to find element with id == //*[#id='ctl00_ContentPlaceHolder1_AddControl1_txtName']
i try to use this:
public IWebElement GetElementId(string id)
{
//return Driver.FindElement(By.Id(id));
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(TimeOut));
return Driver.FindElement(By.Id(id));
}
and tried this:
public IWebElement GetElementId(string id)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement category = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id(el_id));
});
}
I am still couldn't figured how to avoid time-out or element not found error
any help?
Try using the FluentWait class:
public WebElement fluentWait( final By locator ) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class, StaleElementReferenceException.class);
// use a "custom" ExpectedCondition
WebElement foo = wait.until( new Function<WebDriver, WebElement>() {
public WebElement apply( WebDriver driver ) {
return driver.findElement( locator );
}
});
// usually use one of the built-in ExpectedCondtions
// WebElement foo = wait.until(
// ExpectedConditions.visibilityOfElementLocated( locator );
return foo;
};
you can read about fluent wait here
Or if not check thoroughly if you found locator properly.
Hope this helps you)
You are using xpath but in the findElement you are using By.Id change it as
By.xpath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_txtName']")
OR
By.id("ctl00_ContentPlaceHolder1_AddeCardControl1_txtName")
If it still shows timeout error then try by specifying the element name too in the xpath like
//div[#id='element_id']
because specifying like this
//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_txtName']
may took time by searching all the elements id attribute so if you specify the particular element then the searching time will be minimized.
If it doesn't works then check whether that your xpath is correct or not.
Is there a way in the latest version of Selenium DotNet Webdriver (2.22.0) to check to see if an element is visible before clicking/interacting with it?
The only way I've found is to try to handle the ElementNotVisible exception that occurs when you try to send keys, or click on it. Unfortunately this only occurs after an attempt to interact with the element has been made. I'm using a recursive function to find elements with a certain value, and some of these elements are only visible in certain scenarios (but their html is still there no matter what, so they can be found).
It's my understanding that the RenderedWebElement class is deprecated as well other variants. So no casting to that.
Thanks.
For Java there is isDisplayed() on the RemoteWebElement - as well is isEnabled()
In C#, there is a Displayed & Enabled property.
Both must be true for an element to be on the page and visible to a user.
In the case of "html is still there no matter what, so they can be found", simply check BOTH isDisplayed (Java) / Displayed (C#) AND isEnabled (Java) / Enabled (C#).
Example, in C#:
public void Test()
{
IWebDriver driver = new FirefoxDriver();
IWebElement element = null;
if (TryFindElement(By.CssSelector("div.logintextbox"), out element)
{
bool visible = IsElementVisible(element);
if (visible)
{
// do something
}
}
}
public bool TryFindElement(By by, out IWebElement element)
{
try
{
element = driver.FindElement(by);
}
catch (NoSuchElementException ex)
{
return false;
}
return true;
}
public bool IsElementVisible(IWebElement element)
{
return element.Displayed && element.Enabled;
}
It seems the current answer to this question is outdated: With WebDriver 3.13 both the Displayed and Enabled properties will return true as long as the element exists on the page, even if it is outside of the viewport. The following C# code works for WebDriver 3.13 (from this StackOverflow answer):
{
return (bool)((IJavaScriptExecutor)Driver).ExecuteScript(#"
var element = arguments[0];
var boundingBox = element.getBoundingClientRect();
var cx = boundingBox.left + boundingBox.width/2, cy = boundingBox.top + boundingBox.height/2;
return !!document.elementFromPoint(cx, cy);
", element);
}
There is a simple way to do that, follow below:
public bool ElementDisplayed(By locator)
{
new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(condition: ExpectedConditions.PresenceOfAllElementsLocatedBy(locator));
return driver.FindElement(locator).Displayed ;
}
You can use the following:
WebDriver web = new FirefoxDriver(;
String visibility = web.findElement(By.xpath("//your xpath")).getCssValue("display");