Help me to find out the xpath for button "click for JS Alert"
Here is the link https://the-internet.herokuapp.com/javascript_alerts
I am using this xpath-
.//*[#id='content']/div/ul/li[1]/button
and it showing no such element.
This is the code used:
public void test() {
driver.get("the-internet.herokuapp.com/javascript_alerts");
WebElement element=driver.findElement(By.id("//button[text()='Click for JS Alert']"));
element.click();
}
You use code in a wrong way:
WebElement element=driver.findElement(By.id("//button[text()='Click for JS Alert']"));
By.id can be applied to search element with specified id attribute only while you use XPath instead.
You need to use below:
WebElement element=driver.findElement(By.xpath("//button[text()='Click for JS Alert']"));
element.click();
//*[#id='content']/div/ul/li[1]/button works for me. What browser are you using? How are you validating?
Use this xpath: (//div[#class='example']/ul/li/button)[1]
Your code will be somewhat like this:
driver.get(""the-internet.herokuapp.com/javascript_alerts");
WebElement element=driver.findElement(By.xpath("(//div[#class='example']/ul/li/button)[1]"));
element.click();
Try this below code.
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//button[contains(text(), 'Click for JS Alert')]")).click();
Thread.sleep(2500);
Alert alert = driver.switchTo().alert();
System.out.println(driver.switchTo().alert().getText());
alert.accept();
Related
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'm automating some website in which I need to log out. I'm facing a hard time in this code:
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user logout")));
Category_Body.click();
d.findElement(By.id("logout_user")).click();
Thread.sleep(1000);
HTML:
<a class="user logout" title="Sign out" data-target="#confirm_popup" data-toggle="modal"></a>
Error:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"user logout"}
Try the following code for that:
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".user.logout")));
Category_Body.click();
PS: You can do this with ExpectedCondition.elementToBeClickable, also.
Hope it helps you!
I think the issue is with the identifier
You have used
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user logout")));
But according to your HTML
<a class="user logout" title="Sign out" data-target="#confirm_popup" data-toggle="modal"></a>
The link have no id called "User Logout"
With out using id try to use class By.findElementByClassName("user logout")
As a Second solution, try to use xpath ( which will work most of the time )
If both the solutions are not usable you can use the JavascriptExecutor ( The elements which are hard to capture can be easily handled with JavascriptExecutor )
NOTE: The main issue is with you using "user logout" when there is no such ID
Cheers
As per your to locate the desired element to logout within the Model Box you need to induce WebDriverWait for the element to be clickable and you can use either of the the following options:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.user.logout[title='Sign out'][data-toggle='modal']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='user logout' and #title='Sign out'][#data-toggle='modal']"))).click();
There is somthing wrong with the page I want to test.
My first try:
When I clicked manually on a button, then I will be forwarded normally on the next page.
When I tried to click on the same button with selenium, then I get an error page "Sorry...something gone wrong...blabla". I think this problem can only solve the developer team of the page.
By book = By.cssSelector("#button\\.buchung\\.continue");
//By book = By.cssSelector("button.buchung.continue");
//By book = By.xpath("//*[#id='button.buchung.continue']");
WebElement element= ConfigClass.driver.findElement(book);
element.click();
But I want to try a workaround:
I clicked on the same button with JQuery.
I opened my chrome console and execute the button with:
jQuery('#button\\.buchung\\.continue').click()
How can I execute this JQuery expression in my selenium code?
I tried this, but without success:
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("jQuery('#button\\.buchung\\.continue').click()");
Use $
je.executeScript("$('#button\\.buchung\\.continue').click()");
jQuery("selector") will return you a list.
I think you have to call click() on the element at index 0 (Assuming exactly one element satisfies the selector)
Code:
je.executeScript("jQuery('#button\\.buchung\\.continue')[0].click()");
You were pretty close. If the cssSelector is uniquely identifying the WebElement you can use the following code block :
By book = By.cssSelector("#button\\.buchung\\.continue");
WebElement element= ConfigClass.driver.findElement(book);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
I am using selenium webdriver. I am unable to access link menu options.eg:I want to access options "Casual shoes" option under "Men" menu link from flipkart site. i tried using below code
WebElement a= driver.findElement(By.xpath("//a[title='Men']"));
a.click();
but unable to click on menu link "Men"
Your XPath is wrong you forget to add # in front of attribute. You are using //a[title='Men'] but you should use //a[#title='Men']
Below code is working for me:-
driver.get("http://www.flipkart.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[#title='Men']")).click();
driver.findElement(By.xpath("//span[contains(.,'Casual Shoes')]")).click();
OR
In chrome below code is working fine for me:-
WebElement we =driver.findElement(By.xpath("//a[#title='Men']"));
we.click();
WebElement Causual =driver.findElement(By.xpath("//span[contains(.,'Casual Shoes')]"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", Causual);
Hope it will help you :)
Hi I am trying to locate CK editor for my project through Selenium Webdriver code(Java). But Whenever I try to use SendKeys() method it is not working for me.
Below is the screenshot of CK Editor and HTML code.
And below is the code,
if(driver.findElement(By.cssSelector("iframe#scayt_8")).isEnabled())
{
WebElement iframe = driver.findElement(By.cssSelector("iframe#scayt_8"));
System.out.println("Frame Enabled");
if(driver.findElement(By.xpath("//iframe[#id = 'scayt_8']")).isDisplayed())
{
System.out.println("Frame Displayed");
driver.switchTo().frame(iframe);
iframe.clear();
System.out.println("Clicking frame");
iframe.click();
iframe.sendKeys("Hello!!");
}
}
Please help me to locate CK Editor and to Send text to it.
You probably need to switch to the inline frame to locate it.
WebElement editorFrame = driver.findElement(By.id("scayt_8"));
driver.switchTo().frame(editorFrame);
WebElement body = driver.findElement(By.tagName("body"));
body.clear();
body.sendKeys("some text");
We provide techniques for working with editors in chapter 3 of our book Selenium WebDriver In Practice.
I think iframe is being searched based on cssSelector but i think it is supposed to be based on id? which is scayt_8. Can you try with following code to fetch iframe instead of cssSelector:
driver.FindElement(By.TagName("iframe"))
Once you switch to iframe, try to locate webelement by paragraph tag name inside iframe, something like below:-
WebElement body=driver.findElement(By.tagName("p"));
Then try to send keys using this webelement:
body.sendKeys("Hello!!");
WebElement iframe = driver.findElement(By.tagName("iframe")); driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Hello, ckeditor!");;
This will help you to send text in CKeditor. Try this. It will work