#FindBy annotation cannot find the element, when element state is not visible. We are writing some SEO tests those elements are not visible on webpage.
For example following is not working;
#CacheLookup
#FindBy(xpath = "//meta[#name='description']")
public WebElementFacade metaDescription;
But that works;
WebElement metaV2 = getDriver().findElement(By.xpath("//meta[#name='description']"));
It gives an error like;
org.openqa.selenium.ElementNotVisibleException: Timed out after 15 seconds. Element not available
Any idea ?
Thank you
WebElementFacade expects an element to be visible before interacting with it (as do many of the standard WebElement methods). If you want to check an invisible element, use a WebElement or avoid #FindBy entirely, e.g.
By META_V2 = By.xpath("//meta[#name='description']")
.
.
.
$(META_V2).shouldBePresent();
Related
Need to verify element is not able click or Not Clickable using Selenide
Try this:
import static com.codeborne.selenide.Condition.*;
// ...
SelenideElement el = ... // Look up element
el.shouldBe(not(and("Clickable", visible, enabled)));
This is much like how ExpectedConditions detects if an element is clickable.
String selector = ".rmcAlertDialog .buttons :first-child";
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
if (element == selection) selection.click();
But the element in question (a button) is not responding to the click.
If I click the button manually it works so its not the web page at fault, but the automation.
I have verified the button is there by comparing it's text content.
updated for clarification
This code works (or worked) for most buttons. The code is from a script interpreter which is parsing:-
select ".rmcAlertDialog .buttons :first-child" click
This code was working prior to more recent versions of chrome/selenium/chromedriver.
The code now doesn't work for some buttons.
selection.click() IS being called (verified in a debugger), as element will always equal selection, it just is not working.
.buttons is the class name of the container div for the button(s)
The selector is not directing to the element with button class. You have a space between .button and :first-child in the selector. Remove the space. The given selector is searching for a child element of the tag with button class. But I'm assuming you are trying to click on the first element with button class not the child node of the button class element.
Use this:
String selector = ".rmcAlertDialog .buttons:first-child";
I think the main reason it's failing is because your if statement will never be true. I've never done any comparisons like this but you can simplify your code significantly and still get the desired effect.
A few suggestions:
Don't define locators as Strings, define them as Bys. The By class is defined for just such a task and makes using and passing them around MUCH easier.
String selector = ".rmcAlertDialog .buttons:first-child";
would turn into
By locator = By.cssSelector(".rmcAlertDialog .buttons:first-child");
Note the correction that S Ahmed pointed out in his answer.
You don't need to find the element to wait for it to be clickable. There is an overload that takes a By locator, use that instead.
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
becomes
WebElement element = new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(locator));
Skip the RemoteWebElement and WebElement comparison. I don't think this will work and there's no need for it anyway. Your locator will locate the same element consistently.
So your final code should look something like
By locator = By.cssSelector(".rmcAlertDialog .buttons:first-child");
new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(locator)).click();
I have searched but havent found something similar to what Im trying to do. I'm using java by the way, I'm trying to click/access an a tag with selenium. The issue is that I'm not sure how to go about it. There seems to be a function/event that I need to set off but not quite sure how to. I tried a few ways as get text and clicking but I knew that wasn't going to work. Also I seen there are ways of using JavascriptExecutor but not sure how to use it for my case. I will post the tag below and alsothe function signature, that might help. If theres a similar question please post the link.
<a name="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" id="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" ptlinktgt="pt_peoplecode" tabindex="203" onclick="javascript:cancelBubble(event);" href="javascript:submitAction_win0(document.win0,'DERIVED_SSS_SCL_SSS_ENRL_CART$276$');" class="SSSHYPERLINKBOLDSMALL">Enrollment Shopping Cart</a>
the signature
function submitAction_win0(form, id, event)
You don't need any JS. Just use this xpath:
"//a[contains(#onclick,'javascript:cancelBubble(event);')]"
Be sure the element is clickable, see
import org.openqa.selenium.support.ui.ExpectedConditions;
for the case of more matches:
List<WebElement> elements = driver.findElements(By.xpath("//a[contains(#onclick,'javascript:cancelBubble(event);')]"));
int elementIndex = 0; // 0 to get first of the 33 mathes, 32 to get the last one
WebElement element = elements.get(elementIndex);
element.click();
EDIT:
You should use WebDriverWait to avoid NoSuchElementException this way the driver will wait until the element is clickable... it will wait up to 10 seconds you can tell it to wait more if needed...
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Student Center")));
element.click();
Using click():
WebElement element = driver.findElement(By.cssSelector("a[class='SSSHYPERLINKBOLDSMALL']"));
element.click();
Using JavascriptExecutor (Not recommended):
WebElement element = driver.findElement(By.cssSelector("a[class='SSSHYPERLINKBOLDSMALL']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
You can use other locators too... like linkText:
WebElement element = driver.findElement(By.linkText("Enrollment Shopping Cart"));
Or partialLinkText:
WebElement element = driver.findElement(By.partialLinkText("Shopping Cart"));
Shadow Dom structure:
In the above shadow dom structure we are able to access the individual elements using the selenium and javascript as below in chrome:
In Firefox:
//div[#class='style-scope rock-tabs' and not(#hidden)]//div/span[contains(text(),'"+AttrName+"')]/../preceding-sibling::div/paper-icon-button[1]/iron-icon[1]
In Chrome:
We are using the below to navigate to the iron-icon
WebElement Attrbuttona1=Button2.findElement(By.id("contentViewManager"));
WebElement eAttrbutton1=expandRootElement(Attrbuttona1);
WebElement Attrbutton2=eAttrbutton1.findElement(By.id("contentViewManager"));
WebElement Attrbutton2a=Attrbutton2.findElement(By.xpath("rock-content-view[#name='entity-manage']"));
WebElement eAttrbutton2=expandRootElement(Attrbutton2a);
WebElement Attrbutton3=eAttrbutton2.findElement(By.id("content-view-container"));
WebElement Attrbuttona3=Attrbutton3.findElement(By.id("component"));
WebElement eAttrbutton3=expandRootElement(Attrbuttona3);
WebElement Attrbutton4=eAttrbutton3.findElement(By.className("content"));
WebElement AttrTagName2=Attrbutton4.findElement(By.tagName("rock-tabs"));
WebElement eaAttrbutton4=expandRootElement(AttrTagName2);
WebElement Attrbutton5=eaAttrbutton4.findElement(By.id(attrType));
WebElement eAttr1=expandRootElement(Attrbutton5);
WebElement Attr2=eAttr1.findElement(By.className("group-container"));
WebElement Attr3=Attr2.findElement(By.tagName("rock-attribute"));
WebElement eAttr3=expandRootElement(Attr3);
WebElement Attri4=eAttr3.findElement(By.className("attribute-icons"));
WebElement Attr4=Attri4.findElement(By.tagName("paper-icon-button"));
WebElement eAttr4=expandRootElement(Attr4);
WebElement Attr5=eAttr4.findElement(By.tagName("iron-icon"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();",Attr5);
public WebElement expandRootElement(WebElement element) {
WebElement ele = (WebElement) ((JavascriptExecutor) driver)
.executeScript("return arguments[0].shadowRoot",element);
return ele;
}
Now i want to click on iron icon for attribute where div/span[text()='Product Name']
Taking the child element as a base and i need to traverse back to shadow element and get the icon related to only that particular attribute.
How can i proceed in clicking the element based on a different element and traverse back, which should be supported in all browsers(chrome and firefox)?
In JavaScript, if you want to "traverse back" from an element called element1:
To get a parent element, use its parentElement attribute. You can do it recursively if the element you want is higher in the tree.
element1.parentElement
To get the Shadow DOM element, invoke getRootNode() to get the Shadow DOM root and use its host attribute to get its host element.
element1.getRootNode().host
Note: Since it's JavaScript code, it should be executed in Selenium's executeScript() method.
Using firefox and marking a link in my web-app I get among other things, this code which I think I can use to caprture an object:
cb_or_somename_someothername cb_area_0219
This string is "classname" in Firebug.
Going to the script I type in:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername cb_area_0219"));
But the script does not find the element when executing.
Other onfo in the Firebug panel is:
class="cb_or_somename_someothername cb_area_0219"
onclick="jsf.util.chain(this,event,'$(this).attr(\'disabled\', \'disabled\');return true;','mojarra.jsfcljs(document.getElementById(\'fwMainContentForm\'),{\'fwMainContentForm:j_idt156:2:selectRole \':\'fwMainContentForm:j_idt156:2:selectRole\'},\'\')');return false"
href="#"
id="fwMainContentForm:j_idt156:2:selectRole"
Is my script referring the element in a wrong way?
You cannot use search by compound class name (name with spaces). Try to use search by CSS selector instead:
WebElement rolle = driver.findElement(By.cssSelector(".cb_or_somename_someothername.cb_area_0219"));
or by XPath:
WebElement rolle = driver.findElement(By.xpath("//*[#class='cb_or_somename_someothername cb_area_0219']"));
Also you still can use search by one of two class names:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername"));
or
WebElement rolle = driver.findElement(By.className("cb_area_0219")); // Note that this class name could be generated dynamically, so each time it could has different decimal part
Update
If you get Element is not clickable... exception it seem that your element is covered by another element at the moment you try to click on it. So try to wait until this "cover" is no more visible:
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[#class='environtment-banner']"));
WebElement rolle = driver.findElement(By.className("cb_area_0219"));
rolle.click();