I trying to click on the create account button in registration form.
this is how the button locate in the html page:
<div id="submitContainer"><button type="submit" class="large"><span><strong> Create Account </strong></span></button></div>
this is the button xpath:
//*[#id="submitContainer"]/button/span/strong
the problem is that the button don't have id, he locate inside a div.
I try to use by id,xpath,css,name, but all of this not working:
driver.findElement(By.id("submitContainer")).click();
driver.findElement(By.xpath("//*[#id='submitContainer']/button/span/strong")).click();
driver.findElement(By.tagName("Create Account")).click();
driver.findElement(By.className("large")).click();
thanks!
In your examples, except for the last one, you are not targeting the button. Now your last example, should actually locate the button-element:
driver.findElement(By.className("large")).click();
Could you please post the error message you are getting?
Are there more than one element on the page with className "large"?
Make sure the button is in view window, if it is then try clicking on it. Try to wait for the element to load. There might be an issue with your element being loaded into DOM -
driver.wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[#type='submit']"))).click();
Hope this helps.
If you want to use xpath, the correct syntax is
//button[#type='submit']
Use this line below:
Thread.sleep(3000);
I got the result once I used this one. Since some time we need to give some sleep time for the site to load fully to pull the Xpath.
You can use the linkText
driver.findElement(By.linkText("Create Account")).click();
Hope it will work for you.
Related
I am trying to make Selenium click upvote buttons on a reddit-like website. The site has entries from different users and each entry has a upvote and downvote button below it. What i want to do is to make Selenium click on the first upvote button ( which belongs to the entry at the top ) on the page.
I tried to use "Copy XPath" function in Chrome, but all of the upvote buttons on the page return the same XPath:
//*[#id="eksico-chevron-up-thick"]/path
And this is how the website looks like if needed :
So, is there any way for finding the XPath of the first upvote button? I was thinking of something like:
//*[#id="eksico-chevron-up-thick"]/[1]
etc. Thanks in advance.
Edit: The HTML Code of one of the upvote elements:
The element that you are trying to click is under shadow dom as mentioned in the html structure and currently selenium does not support operation on the elements under the shadow dom.
Reference: https://medium.com/rate-engineering/a-guide-to-working-with-shadow-dom-using-selenium-b124992559f
So, if you want to click on the element, you can use JavaScriptExecutor like:
WebElement element = driver.findElement(By.id("eksico-chevron-up-thick"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
By default, it will click on the first element itself and if you want to click on a specific nth element then you can take the elements in a list and then send the index of that element inside the method to get that element clicked.
If you want to click on the first one you could use
Driver.find_elements_by_xpath(//*[#id="eksico-chevron-up-thick"]/path)[1].click()
I am working in a travel application. Once i filled the details like origin, destination i have to click the submit button. I tried with the xpath to click the submit button. But, i am unable to submit. Application is loading internally,once it clicks the xpath of submit. Please refer screen shot.
Selenium code: java driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']")).click();
Please check image
Submitbutton- xpath code
can you please provide your html code for this issue,
there must be element present in that code,
you should have to use submit() instead of clicking on submit element.
actually click() works only on submit button of (you have to check this condition) and submit() works for all form elements and performs Enter key pressed like operations.
i hope this have to work for you,
driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']")).submit();
or try using this,
driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_tpTravel_travel_gvTravel_ctl02_txtDuration']")).submit();
according to your html code we get element by id,
driver.findElement(By.Id("ctl00_cphMain_TravelRequest1_btnSubmit")).submit();
In your case, might be the submit button is visible but not clickable as you mentioned in the description. For that, you will have to wait until the element is not to be clickable using the ExplicitWait method. Perform click action after the Submit button gets clickable:
Hope below code will solve your problem.
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']"))).click();
Sometimes the click() method does not work. Please try with below code once and let me know.
WebElement Login=driver.findElement(By.id("ctl00_cphMain_TravelRequest1_btnSubmit"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", Login);
<button value="1" class="_42ft _4jy0 layerConfirm uiOverlayButton _4jy3 _4jy1 selected _51sy" type="submit">Confirm</button>
The above is the html code for a button on Facebook page. I would like help in selenium web driver to click on this button but the id of the tag is changing on every refresh
Code on facebook website
Thank you all for your help but I have solved this one with help from you all people and trial and error.
Use ExplicitWait until visibility of your element and then perform click
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#type='submit'][normalize-space()='Confirm']")));
driver.findElement(By.xpath("//button[#type='submit'][normalize-space()='Confirm']")).click();
Another solution can be if some javascript preventing you to click the button then use below JavascriptExecutor code and let me know if any issue
WebElement button = driver.findElement(By.xpath("//button[#type='submit'][normalize-space()='Confirm']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", button);
You can use the fixed class names for this
driver.findElement(By.className("layerConfirm"));
Or combination
driver.findElement(By.cssSelector(".layerConfirm.uiOverlayButton"));
If the dynamic id has fixed part you can use partial id
// id=someid134313
driver.findElement(By.cssSelector("[id*='someid']"));
Or by the "Confirm" text
driver.findElement(By.xpath("//button[contains(., 'Confirm')]"));
As Facebook element's class and id are dynamic, you will have trouble automating further with Selenium.
In this case you can go with xpath
//button[#type='submit' and text()='Confirm']
or
//button[text()='Confirm']
You can use following xpath for your case:
driver.findElement(By.xpath("//*[contains(text(),'Confirm')]")).click();
Hope it will help you.
What command in webdriver should I use to make sure it clicks the specific text i.e Last Month
the code is
<div class="dt_padded_medium_div">
<a onclick="setLastMonth()" href="#">Last Month</a>
I tried xpath by using firepath but still doesnt work
it was
//*[#id='block-2']/div/div[3]/table/tbody/tr[1]/td[5]
I used
driver.findElement(By.xpath("//*[#id='block-2']/div/div[3]/table/tbody/tr[1]/td[5]")).click();
but still didnt work, am I missing something?
Update:
Got the Code working guys, thanks for the help!
If there is only one 'a' element in the page with this text, try this XPath:
//a[text()='Last Month']
If there are more than one element, please, post the full HTML tree else we're unable to write a xpath without know the tag path and ensure it will work
Are you sure your xPath is correct?
Install FirePath on Firefox and experiment with your xPath as on screenshot below.
Also, that does look like a long and brittle xPath. Look up xPath cheat sheets to learn how to make your xPath more robust.
In your case I would imagine something like:
//*[#id='block-2']/descendant::a[text()='Last Month']
(get the element with a particular ID, then search for an <a></a> in that element, no matter how deep, with a particular text)
In your question, you have used a "id" that is not shown in your code, where is #id='block-2'. It is possible for you to have used the wrong id. For us to help you, can you please provide the whole HTML code?
What I can suggest based on the information you have provided is:
Making sure the Xpath you have provided is unique, there is an add-on for Firefox called Firebug you can use. It will help you find out the xpath you are after fast and easy. What you need to do is basically:
download firefox and install it;
download firebug add-on and install it to firefox;
you will notice there is a small bug symbol to the top right of your tool bar, please enable it;
you will see a console pops out, click on inspection button and click on any web element you want to inspect and its html code will be highlighted in the console;
right click on the highlighted code and choose to copy its xpath to clipboard. This way you will never get your xpath wrong.
Here is a quick tutorial:
http://www.wikihow.com/Find-XPath-Using-Firebug
P.S. There are more than one way to locate a webelement, please consider using the following options as well:
Css selector
class name
id
This link will direct you to an awesome cheat cheet.
http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
Hope it helps.
Please try to use below code to click on the text Last Month
driver.findElement(By.xpath(".//a[text()='Last Month']")).click();
Hope this helps.
You can use below code to click on the link containing the text 'Last Month'
driver.findElement(By.linkText("Last Month")).click();
First of all make sure that your xpath works correctly, try to use more specific xpath other than just td[5] or smthn like that, for example above could be like:
//div[#class='dt_padded_medium_div']/a[#href] - meaning, we need to find <div> element with specific parent and within it <a> element that has href property
There are a few possibilities.
If you're site doesn´t get rewritten to much you could just go for the link name via
driver.findElement(By.linkName("Last Month");
You could also go for any of the attributes if your site is subject of many rewrites like this and just put "href" and "#" or "onclick" and "setLastMonth()" as arguments when calling it.
static WebElement getLink(String Attribute, String Value /*String ItemText*/){
List<WebElement> Elements = driver.findElements(By.tagName("a"));
for(int Counter = 0, Counter < Elements.size(); Counter++){
if(Elements.get(Counter).getAttribute(Attribute).contains(Value) /* && Elements.get(Counter).getText().equals(ItemText){
return Elements.get(Counter);
}
}
return null;
}
If you remove the comments it will go completely sure and check for the Attributes and the displayed text.
Apologies for this;
Event Triggers
Photos
Push Notifications
How can I Click on Event triggers for example using Selenium Java?
I've tried this code:
By.xpath("//button[contains(text(),'Event triggers')]");
By.xpath("//button[contains(text(),'Submit')]");
I tried this as well:
WebElement Box= driver.findElement(By.tagName("Event triggers"));
Box.submit();
Neither of these worked...
Thank you
Try to using this:
By.xpath("//a[contains(text(),'Event triggers')]");
instead of
By.xpath("//button[contains(text(),'Event triggers')]");
Based on the page source mentioned in the comment I think Event Triggers is not a button but an tag which essentially makes it a link.
As you know the text of the link you are trying to click you can always use:
driver.findElement(By.linkText("Event Triggers"));
to get Event Triggers web element.
Using xpath should be avoided due to slow performance.
Well, if button doesnt have any id , you can try to write dynamic xpath by using "class name" and "type" (there are plenty of examples , you can learn easily how to create your xpath) , or easiest way, use firebug to locate element that you want to click and copy exact Xpath via firebug. And then click.
The correct answer is:
By.xpath("//button[contains(text(),'Event triggers')]").click();
You were missing that click action.