Well I have below code
<button class="jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2" data-ember-action="" data-ember-action-689="689">Search</button>
I want to find this element in selenium and perform click action. I tried several options like by class, xpath, name, text, contains but nothing worked.
Can someone guide me here?
driver.findElement(By.xpath("//button[contains(.,'Search']")).click();
driver.findElement(By.className("jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")).click();
driver.findElement(By.className("//*[#id=\"ember689\"]/button")).click();
driver.findElement(By.linkText("Search")).click();
To summarize what was in the comments. Each locator had something off.
By.xpath("//button[contains(.,'Search']")
was missing a parenthesis and needed to be:
By.xpath("//button[contains(.,'Search')]")
Meanwhile, because By.className expects a single className
By.className("jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")
also does not work. (see github.com/seleniumhq/selenium/issues/1480
but could as:
By.cssSelector(".jobs-search-box__submit-button.artdeco-button.artdeco-button--3.ml2")
Also
By.className("//*[#id=\"ember689\"]/button")
refers to an id not presented (Also, I'm not sure, but I think would need to be by xpath).
By.linkText("Search")
does not work because there is no tag a and so no hyperlink.
In Protractor this is much simpler because you would just say by.buttonText('Search')
You can achieve the same things by using javascript. kindly find the below example of code:
//Creating the JavascriptExecutor interface object by Typecasting
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement button =driver.findElement(By.xpath("//button[#class='jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2']"));
//Perform Click on LOGIN button using JavascriptExecutor
js.executeScript("arguments[0].click();", button);
I hope it will work on your case.
Note: Make sure your element will be static.
The correct XPath locator would be:
//button[text()='Search']
If you won't be able to locate it using the above query, make sure that:
The button doesn't belong to and <iframe>, if this is the case - you will have to change the context using switchTo() function
The element is present in DOM, i.e. the page has been loaded fully. It's better to use Explicit Wait for element location/interaction like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Search']")));
More information: How to use Selenium to test web applications using AJAX technology
Try with These two hope it works,
1.) Using Contains
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Search')]")));
2.) Using CSS
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.contains('Search')")));
If does not work let me know i'll provide another Solution.
Related
I have this code:
WebElement iframeElement = driver.findElement(By.xpath(xpIframe));
driver.switchTo().frame(iframeElement);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpLucka)));
driver.findElement(xpLucka).click(); //this click fails
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpSvara))); //TimeoutException
driver.findElement(xpSvar).click();
driver.findElement(xpSvara).click();
Frequently it fails on the line with the comment //TimeoutException. When I look at the state of the web page in the browser that is left open it is clear that the click on the line before it has failed. This is confusing. The element clearly is there, I find it without problems in the web inspector and the wait.until on the previous line obviously succeeds.
The next thing I want to make sure doesn't fail is the switchTo() statement. How can I verify a switchTo-call?
Note that is also succeeds frequently. I just ran this in a loop 9 times, it failed 5 times "but" succeeded 4 times.
Any other suggestions why this might happen are of course very welcome.
To click() on an element within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use the following Locator Strategies:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("xpIframe")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("xpLucka"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
I faced some difficulties within my automation testing that WebDriverWait is not always working. As you may already know that Thread.sleep(3000); is not a recommended approach to use when it comes to automation testing, however sometimes you must use it. So for the testing purposes i would intentionally use:
Thread.sleep(3000); --> add some time, to make sure it switched to iframe
WebElement iframeElement = driver.findElement(By.xpath(xpIframe));
driver.switchTo().frame(iframeElement);
Thread.sleep(3000);
driver.findElement(xpLucka).click(); --> see if it will click on element
Thread.sleep(3000);
driver.findElement(xpSvar).click();
Thread.sleep(3000);
driver.findElement(xpSvara).click(); --> same for all other elements
There is a button on a website I am testing and Selenium can't seem to click it. I have tried many different ways but still nothing. Attached is the HTML for the button as well as the XPath.
Has anyone else experienced this or knows how to get around this?
XPath:
/html/body/div[1]/div/div/form/div[21]/div[3]/button
[HTML]
Could be a synchronization issue. kindly try below solution if your element is not within iframe or else you need to switch control to iframe first before dealing with the web element.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"))).click();
Or You can also try javascript solution:
WebElement element= driver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Try below code -
Actions action = new Actions(driver);
WebElement My_btn = webdriver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
action.moveToElement(My_btn).click(My_btn).build().perform();
Let me know the outcome.
I am getting a very long xpath for an element that I selected. Is there anyway to shorten it? This is the xpath I am getting:
//li[#class='menu_men 1-level hasChild']//div[contains(#class,'level-2')]//div[#class='menu-wrapper']//ul[#class='level-2']//li[#class='1-level']//div[#class='level-3']//ul[#class='level-3']//li//a[#class='level-3'][contains(text(),'Socks')]
This is the URL: Calvin Klein Singapore I hovered over 'MEN', the accessories section will appear, than I hover the 'Socks' to get the xPath.
I am getting the following execption in my code and I am wondering if somehow the long xpath could be one of the reasons:
org.openqa.selenium.NoSuchElementException: no such element: Unable to
locate element: {"method":"xpath","selector":"//li[#class='first
menu_men 1-level
hasChild']//div[contains(#class,'level-2')]//div[#class='menu-wrapper']//ul[#class='level-2']//li[#class='1-level']//div[#class='level-3']//ul[#class='level-3']//li//a[#class='level-3'][contains(text(),'Socks')]"}
I am using cropath from within chrome developer tools to get the xPath.
I am new to automation, I really hope someone can advise. Thank you.
#SameerArora this is the code I have to clear the pop up window, as what I had mentioned in the comments below.
//for clearing the popup window
#FindBy(how=How.XPATH,using="//*[starts-with(#id,'popup-subcription-closes-link-')]")
public WebElement newsletterpopup;
public String clickCategory(){
//.....
resusableFunctions.buttonClick(driver, newsletterpopup, "popoup");
}
public void buttonClick(WebDriver driver, WebElement element, String elementName) throws InterruptedException
{
try
{
element.click();
System.out.println("Log: ResuableFunction.buttonClick");
}
catch (org.openqa.selenium.ElementNotInteractableException notInteract)
{}
The element you are looking for can be found using xpath:
WebElement element = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
However, as the element is not visible directly when you are opening the link, you would be getting NoSuchElementException, so to resolve it you can use javascript click method on the element which directly operates on the div of the page.
Addition to this, i can see that a subscription popup comes when i am opening the page for the first time, so you need to dismiss that popup first(if the popup is present) and then click on the "Socks" element using the JavaScript click method.
Your code should be like:
List<WebElement> closeSubscriptionPopUp = driver.findElements(By.xpath("//a[contains(#id,'popup-subcription-closes-link')]"));
if (closeSubscriptionPopUp.size() > 0) {
closeSubscriptionPopUp.get(0).click();
}
WebElement sockElement = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", sockElement);
To hovered over 'MEN' >> accessories >> 'Socks' section, You need to use selenium Actions class.
As it is not really possible to first click on men(as it will open other section),
So to hover to sock, you need to chain all of the actions that you want to achieve in one go.
Process should be:
move to men element first
Move to accessories
then move to Socks and click on it.
Note: By using Action class, we can chain all the process in one single go.
As mentioned below
1) First way:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]")))
.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")))
.click().build().perform();
2) Second way with wait:
WebDriverWait wait= new WebDriverWait(driver, 10);
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]"))).build().perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.click().build().perform();
Try this:
//a[normalize-space(text()) = 'Socks']
I would recommend you to not use such long xpath's and try to write xpath on your own.
Try :
//li[contains(#class,'menu_men')]//a[contains(text(),'Socks')]
I tried everything. By xpath , by css-selector , by class name too.
//*[#id="opbox-listing"]/div/div/section[2]/section/article[5]/div/div/div[2]/h2/a
thats look xpath , but don't work
on selenium i tried thats way:
driver.findElement(By.xpath("//*[#id=opbox-listing']/div/div/section[2]/section/article[5]/div/div/div[2]/h2/a")).submit();
driver.findElement(By.xpath("//*[#id=opbox-listing']/div/div/section[2]/section/article[5]/div/div/div[2]/h2/a")).click();
what i do wrong? someone have any ideas?
Your xpath is broken: opening apostrophe is missing for an id value:
"//*[#id='opbox-listing']/div/div/section[2]/section/article[5]/div/div/div[2]/h2/a"
Try out clicking on the element using one of following methods
Method 1:
Actions action = new Actions(driver);
action.moveToElement(<your WebElement>).click().perform();
Method 2:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", <your WebElement>);
These are just workarounds. Please provide the exception that you are getting while performing the normal selenium click operation may be that can help you find an answer.
I want to click the load More link in a page. My code is below.
pageUrl="http://www.foundpix.com/category/actor/bollywood-actor/"
WebDriver driver = new FirefoxDriver();
driver.get(pageUrl);
driver.manage().window().maximize();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,2500)", "");
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("json_click_handler")));
driver.findElement(By.id("json_click_handler")).click();
How can I make it click the link.
You can use below xpath to click Load More button both the times:-
driver.findElement(By.xpath("//*[#id='blocks-left']/div/div[3]/div[contains(.,'Load More')]")).click();
this button change the location after you click on it and it can be clicked twice so:
before the first click use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div")).click();
after first click,you can use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div[2]")).click();
Maybe come at it from a different angle. Do you really need to have the link clicked or do you have some Javascript function that gets called on click of the link (like window.loadMore). Can you call the function directly? Selenium is a bit annoying in the sense you can only click a visible element (I don't mean it has to be in the viewport - it just can't have a style like display:none;).