How to click Onclick Javascript form using Selenium? - selenium

This is my source code :
<div class="emph a-center addCard"
Or
<a class="add-link" href="javascript:;" onclick="gotoPaymentAddressForm()">Add a New Credit Card</a
</div>
I need to click "Add New Credit" card which open form on the same page. Unable to do this using Selenium.
Kindly advice.

Try with following:
driver.findElement(By.linkText("Add a New Credit Card")).click();

You can also use following xpath codes:
driver.findElement(By.xpath("//a[#onclick='gotoPaymentAddressForm()']")).click();
or
driver.findElement(By.xpath("//a[contains(text(),'Add a New Credit Card')]")).click();

CSS selector is faster than XPATH so you can try
driver.findElement(By.cssSelector("a.add-link")).click();

Related

Xpath not finding element (parent/ancestor)

<div class="slds-show" data-aura-rendered-by="10155:0">
<div class="footer" data-aura-rendered-by="10156:0">
<div class="slds-grid slds-grid--align-end slds-m-top--large" data-aura-rendered-by="10157:0">
<div class="slds-show" data-aura-rendered-by="10158:0">
<button class="slds-button slds-button--neutral slds-m-left--small" data-aura-rendered-by="10159:0">Cancel</button>
<button class="slds-button slds-button--neutral slds-m-left--small" data-aura-rendered-by="10161:0">Save & New</button>
<button class="slds-button slds-button--brand slds-m-left--small" data-aura-rendered-by="10163:0">Save</button>
</div>
</div>
</div>
This is part of page, on which I will have to click on Save button.
Button is not unique and I need to find it throu class attribute from first div (slds-show), or
Can somebody tell me, why this xpath is not finding this element?
//button[parent::div[#class='slds-show'][#class='slds-button slds-button--brand slds-m-left--small']]
I've also try with ancestor, text instead of class and results is the same. Element is not found via Firefox console
To click on Save button once finding it through class attribute from first div (slds-show) you can use a much simpler and effective xpath as follows :
//div[#class='slds-show']/button[#class='slds-button slds-button--brand slds-m-left--small']
Note : The class attribute slds-button--brand is unique for the Save button.
Try to update your expression as below:
//button[parent::div[#class='slds-show'] and #class='slds-button slds-button--brand slds-m-left--small']
Note that predicate [#class='slds-button slds-button--brand slds-m-left--small'] in your XPath intend to test #class value of parent div, but not target button
You can try the following xpaths.
//*[#class="slds-show"]/button[text()="Save"]
or
//*[class="slds-show"]/button[#class="slds-button slds-button--brand slds-m-left--small"]
An xpath can easily get too complex, you can also try something like this:
//button[text()='Cancel']
//button[text()='Save & New']
//button[text()='Save']
These will return the exact buttons you need. If you're looking for a specific ancestor, include it in your xpath:
//div[#class="slds-show"]//button[text()='Save & New']

How to Click On Group Add Member through facebook

I want to click on group add button of Facebook, but I have not found it possible using an xpath.
Here is the html for the button:
<li>
<a
class="_42ft _4jy0 _3-8i _4jy3 _517h _51sy"
role="button"
href="#"
ajaxify="/ajax/groups/members/add_get.php?group_id=1168192579894018&refresh=1"
rel="dialog">
<i class="_3-8_ img sp__lkuGKPb9f- sx_e8790e"></i>
Add
</a>
</li>
This is how I have tried to click on it:
JavascriptExecutor jse1 = (JavascriptExecutor)driver;
jse1.executeScript("document.getElementById('gbqfb').click‌​();");
You should try using xpath as below :-
driver.findElement(By.xpath(".//a[contains(.,'Add')]")).click();
Or
driver.findElement(By.xpath(".//a[normalize-space() = 'Add']")).click();
I don't see the id that you are using.
You can try with the following css selector:
a[href*='groups/members/add'][href*='116819257‌​9894018']
The first href assure that you are clicking the add for the group,
the second href assures that you are clicking add for the right group
I don't know how the page is looking, if you have only one add then you can remove the second href.
If if you can get the selector with css then for sure you can get it with xpath.
I recommend using css.

How to find the onclick field using firefinder?

I have the following HTML snippet,
<a onclick="courseOfferingClose(); return false;" href="">
Close
</a>
In this snippet, I'm trying to find the onclick field using firebug firefinder. I have tried the following,
[onclick*='courseOfferingClose']
a[contains(#onclick,'courseOfferingClose')]
[contains(#onclick,'courseOfferingClose')]
But none of these work... Any ideas please
Try css-selector,
a[onclick*='courseOfferingClose']
or Xpath,
//a[contains(#onclick, 'courseOfferingClose')]

unable to find the button below using selenium java webdriver

i'm using selenium, but unable to find the button below using findElement(By.xpath ... Could someone provide some help? Thanks very much!
<button type='button' class='pzhc' data-ctl data-click='[["processAction", ["LookUpPreview","true",":event","","Rule-HTML-Section"]]]' ><div class='pzbtn-lft' ><div class='pzbtn-rgt'><div class='pzbtn-mid' data-click='...'><img src='webwb/zblankimage.gif' class='pzbtn-i'/> Lookup</div></div></div></button>
Use the class name:
WebElement element = driver.findElement(By.className("pzhc"));

selenium and clicking an "a" with href=javascript

I'm trying to click a link and am having difficulties. The relevant HTML code is:
<div id="adHocAddDocDiv" style="display: block;">
<a href="javascript:hideDiv();" style="color:#000">
Close window
</a>
<table border="0">
<tbody></tbody>
</table>
</div>
For code, I have:
driver.findElement(By.xpath("//*[#id='adHocAddDocDiv']/a")).click();
This does find the correct element, however it doesn't seem to execute the JavaScript to close the window that happens if I manually click the link. Any ideas?
UPDATE: Here is the code that finally worked:
WebElement element = driver.findElement(By.xpath("//[#id='adHocAddDocDiv']/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
I frequently come across elements that WebDriver doesn't seem to be able to click. In these cases I use the following pattern:
var js = (IJavaScriptExecutor)driver;
js.ExecuteScript("$j(\"div[id='adHocAddDocDiv']\").click();");
This is the C# version. I'm sure the Java form is quite similar.
Try more explicit:
driver.findElement(By.linkText("Close window")).click();
My guess is that there are more <a>'s immediately following that div, and it's not unique enough. Try this:
driver.findElement(By.cssSelector("div#adHocAddDocDiv > a[href*='hideDiv()']")).click()