Webdriver: finding and clicking hidden element, where id constantly changing - java

i am newbie to selenium webdriver, i am struggling to find a button on the webpage, because its Id is constantly changing and there is no other attributes available to use to identify this element. please see below code.
could someone look at this code and help me how to identify this button element.
selenium webdriver hidden elements

You should try by xPath as below :-
WebDriverWait wait = new WebDriverWait(driver, 1000);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xPath("//div[contains(#class, 'header-navigation__cta-buttons-wrapper')]/a[contains(#class, 'cta-buttons__link--button-1')]")));
el.click();
Be sure this element is not inside a frame..
Hope it will help you..:)

Related

Selenium webdriver (java) - Click on desired link of some search results

I am doing automation using selenium webdriver (java) on a search engine BookMyCrop (http://www.bookmycrop.com). Here, I searched for a crop but, I am not able to click on desired search result. Please help me with it.
Code :
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
------My 1st try-------------
//WebElement link = driver.findElement(By.xpath("\"//div[#id = 'Links']/a[3]\""));
//link.click();
------My 2nd try-------------
//List<WebElement> find = driver.findElements(By.xpath("/html/body/section[2]/div[2]/div/div/div/div[1]"));
//find.get(1).click();
}
} –
You can use the css selector based on class names: ".product-block.inner-product-block" and get the list of all the search results.
Then click on whatever index you want to click.
I am not using an IDE for this but it would look something like this:
driver.findElements(By.cssSelector(".product-block.inner-product-block")).get(0).click();
As said, you can try with css ".product-block.inner-product-block"
Then
get List of WebElements
do loop
inside loop, try get text of each element or innerText attribute
cross check if it is required one or not by simple if condition
If so, click on that web element and break loop
if this locator is not giving required info, try other locator. say $$("a h3") for veg names.
The below code worked for me. It is navigates to the correct link
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.bookmycrop.com");
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();

Selenium not able to input element in the form inside fieldset tag

I have tried a different method to input elements like
Here is xpath I used
By NameOfTheProperty=By.xpath("//fieldset/div/input[#name='name']");
By NameOfTheProperty=By.xpath("//div/input[#name='name']");
By NameOfTheProperty=By.xpath("//input[#name='name']");
Tried with Selenium Builder
WebElement element=driver.findElement(by);
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(element)
.click().sendKeys(text).build();
mouseOverHome.perform();
Tried with
WebElement element=driver.findElement(by);
element.sendKeys(text);
None of the methods is working..I can not able to input text inside the field and It shows
Element not interactable
Here is the site
I hope someone help me to find out the solution..
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
xpath you should checks is :
//input[#name='name']
if it is unique, then you can use Javascript executor :
WebElement password_input = driver.findElemenet(By.xpath("//input[#name='name']"));
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', 'password_should_be_written_here')", password_input)
personally I would use id's if the element has one.
"//*[#id='__BVID__104']"
Make sure you are waiting for the element to be ready.
WebDriverWait wait = new WebDriverWait();
wait.until(ExpectedConditions.elementToBeClickable(element));
If it then times out on wait.until(... then I've found that sometimes an element must first be clicked for it to be exposed.
If this is the case. You will have to inspect the element before it is clicked. Find it's xpath, and see if it changes when it is clicked. If so then create a webElement for that element and have Selenium first click it, before clicking the actual field/element.

Cant automate Amazon.com signout using Selenium Java

I'm trying to do a simple selenium java automation for automating "sign in and sign out of amazon.com site". I'm able to sign in using element locator techniques, like XPath and CSS selector. But for signout, I'm thrown with ElementNotInteractable exception.
Below is the code that I tried(posting the code segment of signout alone).
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).click();
I have tried the above code segment with different element locator techniques like CSS selector and etc, but no luck.
Kindly suggest if I can find and click the sign-out link in the flyout menu by any other method.
Thanks.
You can try below code in which explicit wait is implemented so it will wait for the element to click
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element1 =driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-
belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
ele2=driver.findElement(By.xpath("//a[#id='nav-item-signout']"))
wait.until(ExpectedConditions.elementToBeClickable(ele2));
ele2.click();
Instead of the click() method try this :
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.sendKeys(Keys.RETURN);
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).sendKeys(Keys.RETURN);
And instead of RETURN you can also try ENTER
You can try below code in which mover hover is implemented so it will hover the menu then you can click for sign-out.
WebElement ele = driver.findElement(By.id("nav-link-accountList-nav-line-1"));
Actions action = new Actions(driver);
action.moveToElement(ele).perform();
driver.findElement(By.xpath("//*[#id='nav`enter code here`-item-signout']/span")).click();

cannot click on Agree & Continue button in Paypal CheckOut using Selenium

I am using selenium for my testing a retail website . Once I reached the Checkout page , I am selecting the option as Paypal Where a sandbox url is opening.
https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-07L974777B231831F#/checkout/login
I am able to enter the username and password , clicked on Login button.
After that I am redirected to "Agree & Continue " Page . Where I could not perform any action.
I could see clearly the button properties as below
I have tried the below code, but could not perform any action.
WebElement AgreeandContinue= driver.findElement(By.tagName("input"));
AgreeandContinue.click();
You might have many input elements in the same page. What about trying to select by class or id?
WebElement AgreeandContinue= driver.findElement(By.ByClassName('btn'));
or
WebElement AgreeandContinue= driver.findElement(By.ByClassName('continueButton'));
then using submit() instead of click() as the element is a 'submit' type`
FYI : https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html
Looks like the button has an id defined, so that would be the best locator to use: driver.findElement(By.id("confirmButtonTop));
If that doesn't work, then you might have to add some waits for the button to be clickable.
And if that still doesn't work, then it's possible, as it is with many commercial tools, that the button is actually inside a different iframe. Look further up in the html to confirm if this is the case (it'll have a iframe tag). If that's the case, then you'll have to first switch to the iframe before clicking the button: driver.switchTo().frame(...) . How to identify and switch to the frame in selenium webdriver when frame does not have id
If we look at the HTML you have provided the WebElement with value Agree & Continue is within an <input> tag. So we have to construct a unique css or xpath to identify the WebElement as follows:
cssSelector :
WebElement AgreeandContinue= driver.findElement(By.cssSelector("input#confirmButtonTop"));
OR
xpath :
WebElement AgreeandContinue= driver.findElement(By.xpath("//input[#id='confirmButtonTop']"));
Try with ID, but if it is not you can try with other locators , I suggest you have to use xpaths:
driver.findElement(By.xpath("//input[contains(#id,'confirmButtonTop')]")).click();
or
driver.findElement(By.xpath("//*[contains(#id,'confirmButtonTop')]")).click();
also I will suggest that you can use wait until element to be clickable or visible
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#id,'confirmButtonTop')]")));
driver.findElement(By.xpath("//input[contains(#id,'confirmButtonTop')]")).click();
or
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(#id,'confirmButtonTop')]")));
driver.findElement(By.xpath("//*[contains(#id,'confirmButtonTop')]")).click();

selenium.ElementNotVisibleException: Element is not currently visible JAVA

I am working on selenium, while running Java code I tried to CLICK a menu from the web page but encounter error of selenium.ElementNotVisibleException: Element is not currently visible Kindly advise on this matters . Thanks you
HTML code for text field :
<li onclick="goin('pages/AbcProxy/proxyGroupList.do')>
TESTABC
JAVA code:
WebdriverWait wait = new WebDriverWait(driver,50);
wait until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div/ul/li[12]/a")));
presenceOfElementLocated checks if the element exists in the DOM. To check if the element is visible use visibilityOfElementLocated
WebdriverWait wait = new WebDriverWait(driver,50);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div/ul/li[12]/a")));
element.click();
It might be that your xpath is to a different a element that is invisible (css display none or such).
It would be better to use some id first (maybe for the parent ul?) and then a relative xpath from there, full xpaths from root are not recommended as it makes the test very brittle

Categories

Resources