Selenium Java: Unable to Locate Element inside #document section - java

Did anyone experience handling the element which is inside the #document section?
I had tried for absolute xpath, JavascriptExecutor, shadow, switchTo but still not able to locate the element.
the html structure as shown in the pic
html typing version

I'm able to locate the element by using switchTo method
driver.switchTo.frame(driver.findElement(By.tagName("object")));
driver.findElement(By.cssSelector("input[name='custId']")).sendKeys("customer123");

Related

Unable to locate elements on a website , i tried all the locators but I get the error "No such element" . I am a self taught beginner to Automation

Element HTML:
Inbox
What I tried:
driver.findElement(By.cssSelector("a[text='Log out']"));
then
driver.findElement(By.xpath("//a[.='Log out']"));
Element snapshot:
HTML
driver.findElement(By.linkText("Log out"));
Something like that should work, you should provide the page html for a better response.
My response is based on the fact that in your first try you are saying text='Log out'.
findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.
Let me know if it works, otherwise provide me the web page html snippet.
I've seen the actual screen, you must change Log out with Inbox
driver.findElement(By.linkText("Inbox"));
Given the HTML:
Inbox
You need to take care of a couple of things here as follows:
Within cssSelector doesn't supports the :contains("text") method
Within xpath for exact text matches you need to use text()
Solution
To identify the element you can use either of the following locator strategies:
Using linkText:
WebElement element = driver.findElement(By.linkText("Log out"));
Using cssSelector:
WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
Using xpath:
WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and #title='View the Inbox']"));

Unable to locate element using className in Selenium and Java

I wanted to locate a element of a web page using class name in Selenium. This is the web element that I tried:
<button class="signup-button b green">Get Started!</button>
When I try this way, I was unable to locate the button;
driver.findElement(By.className("signup-button")).click();
But, using css selector like below, it was working;
driver.findElement(By.cssSelector("button.signup-button")).click();
What is the reason for that sometimes working and other times not working?
As you are able to locate the following element:
<button class="signup-button b green">Get Started!</button>
using:
driver.findElement(By.cssSelector("button.signup-button")).click();
but was unable to locate the same element using:
driver.findElement(By.className("signup-button")).click();
That's because there were other elements which renders within the HTML DOM with className as signup-button even before the desired element, which possibly may be invisible by design.
Ideally, you should also be able to use a xpath based Locator Strategy:
driver.findElement(By.xpath("//button[#class='signup-button' and text()='Get Started!']")).click();
Best Practices
But as per best practices, you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.signup-button"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='signup-button' and text()='Get Started!']"))).click();
References
You can find a couple of relevant discussions in:
NoSuchElementException, Selenium unable to locate element
You need to use relative xpath , if you could not have locators like id and class name
Can you try with //button[contains(text(),"Get Started!")] this xpath

Unable to locate element in Selenium webdriver 2.0

I am unable to locate this element with the class name. Below is the HTML code:
<a class="j-js-stream-options j-homenav-options jive-icon-med jive-icon-gear" title="Stream options" href="#"></a>
I tried to create an xpath using class and title both of them did the work in eclipse...ex:
//a[#title='Stream options']
//a[contains(#class,'j-js-stream-options j-homenav-options jive-icon-med jive-icon-gear')]
..
the None of the above options worked and I tried a few others too...Essentially I want to click on this element and do some action..I want to locate the randomly created xpath so that I can click on the element in the next run.
FYI: the element is a hidden element I need to click on some other element before this element appears. This is a dynamically created element whose expath changes all the time.
Any suggestions would be greatly appreciated...Thanks
Is the element you want to select in a separate iframe? If so, you need to switch to the correct iframe (driver.switchTo().frame("frame-id")) before firing the xpath selector.
Additionally, something to watch out for is that old versions of IE didn't have a native xpath library. See this answer for more details.

How to find an element inside a tablelayout using xpath in appium for android

How to find an element inside a tablelayout using xpath in appium for android. I have tried the following syntax
WebElement username=driver.findElement(By.xpath("//android.widget.TableLayout[#index='1']//android.widget.TableRow[#index='0']/android.widget.EditText[#index='0']"));
username.sendKeys("sampletext");
but I am getting the following exception.
"org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters."
When I tried the following
WebElement username=driver.findElement(By.xpath("//android.widget.EditText[#index='0']"));
It is working fine. The problem arises when I tried to add the parent followed by the child.
1.How can i access all the childs of table layout using a for loop?
2.How can I find a child followed by parent using xpath.Pls help
Try using:
WebElement username=driver.findElement(By.xpath("//TableLayout[#index='1']/TableRow[#index='0']/EditText[#index='0']"));

How to fill The form in a popup

I'am trying to automate http://rose.99ats.com/careers.aspx
After clicking on "signup", I couldn't find the element Popup. I used getWindowHandle(), also used driver.swithchto(), but getting error. I can't find element.
Because it's in a new iframe, you need to use driver.switchTo().frame().
Here is a detailed answer on using switchTo(), in which you can't use name/id in your case, index should generally be avoided, so you may try locate the iframe element by css selector or xpath first, then switch to this WebElement.
WebElement popup = driver.findElement(By.cssSelector("iframe[src^='CareerSignUp.aspx']"));
driver.switchTo().frame(popup);
// or by index: driver.switchTo().frame(0);

Categories