How do I locate the button element and click it - selenium-webdriver

<button class="sfc-value sfc-js-change sfc-js-field sfc-ui-btn fo-ui-btn fo-ui-container fo-ui-fixed style-114 layout-156 sfc-js-click" type="button">
<span class="sfc-caption fo-ui-label style-115 layout-157 fo-ui-no-icon">Next</span>
</button>
I am new to selenium and I am confused with the classes to target in here. So any best way to target the element .I need to click the button here.

You create an xpath that starts with the span using the text contained, then refer -back- to the button itself:
driver.findElement(By.xpath("//span[text()='Next']/..")).click();
**edited

To desired element is a dynamic element so to target the element and click it you have to induce WebDriverWait for the desired element to be clickable and you can use either of the following (Java) solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.sfc-value.sfc-js-change.sfc-js-field.sfc-ui-btn.fo-ui-btn.fo-ui-container.fo-ui-fixed.style-114.layout-156.sfc-js-click>span.sfc-caption.fo-ui-label.style-115.layout-157.fo-ui-no-icon"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='sfc-value sfc-js-change sfc-js-field sfc-ui-btn fo-ui-btn fo-ui-container fo-ui-fixed style-114 layout-156 sfc-js-click']/span[contains(text(),'Next')]"))).click();

Related

Having difficulty locating an element using ID, XPATH and CSS

Unable to locate an anchor element present inside an li element using Selenium webdriver.
For the below HTML DOM structure, I am unable to locate the second li element. I basically want to locate it and perform click operation on it there by loading in a new page using Selenium Webdriver.
Tried to access it using the below methods:
id - docTab
xpath -.//div[#class='secondary-tabs']/ul/li[1]/a
xpath - //*[#id="docTab"]
xpath - /html/body/div[1]/main/div[2]/div/div/div[2]/div[1]/ul/li[2]/a
css - #docTab. tab__heading
css - li[id="docTab"]
But all of it are resulting in "Unable to locate element" error. This is happening even after including driver.wait methods.
DOM Structure:
<div class="secondary-tabs">
<ul class="tabs tabs--bordered">
<li class="tab active second_tabs" id="textTab" tabdivid="textSecond"><a><div class="tab__heading" title="Text">Text</div></a></li>
<li class="tab second_tabs" id="docTab" tabdivid="docSecond"><a><div class="tab__heading" title="File">Document</div></a></li>
<li class="tab second_tabs" id="faqTab" tabdivid="faqSecond"><a><div class="tab__heading" title="FAQ" onclick="FAQ">FAQ</div></a></li>
</ul>
</div>
I expect to locate the second li element and perform click event on it using selenium web driver.
To click() on the element with text as Document you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
Java:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.secondary-tabs>ul li#docTab[tabdivid='docSecond']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='secondary-tabs']/ul//li[#id='docTab' and #tabdivid='docSecond']"))).click();
You can try below given selector.
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='secondarytabs']//child::ul//child::li[#id='docTab' and tabdivid='docSecond']"))).click();

Element not visible with button click in selenium chrome driver

I'm trying to click the continue button after filling in the fields on this webpage. But an exception is thrown saying element is not visible even though I maximize the screen and you can clearly see the button.
I have tried the following even with waiting 10 seconds for the page:
driver.findElement(By.xpath("//*[#id=\"submitButton\"]/span")).click();
driver.findElement(By.cssSelector("#submitButton > span")).click();
driver.findElement(By.partialLinkText("Continue")).click();
driver.findElement(By.className("caret_rebrand")).click();
driver.findElement(By.name("submitButton")).click();
driver.findElement(By.xpath("//span[contains(#class,'red') and contains(text(), 'Continue')]")).click();
Here is the part of the html I am trying to access:
<button style="padding-left: 0px;" type=button" "id=submitButton" class = "nbutton" title = "Continue" onclick=_hblicnk('continue,'continue'); goFeaturePage('true');">
<span style = "padding-right: 12px;" class="red"
"Continue"
<img class="caret_rebrand">
</span>
I expect the continue button to be found and clicked. attached is the picture of the webpage
UPDATE: 8-3-19: I've tested the following pieces of code and it is able to find the element in all cases. But when adding the .click() function to any one of them, it causes a no such element exception.
driver.findElement(By.name("submitButton")).click();
driver.findElement(By.id("submitButton")).click();
driver.findElement(By.cssSelector("#submitButton")).click();
driver.findElement(By.xpath("//*[#id=\"submitButton\"]")).click();
My expectation is that you need to click the <button> element as this <span> might be not clickable at all. You can use i.e. title attribute to uniquely identify the element on the page
It's better to use Explicit Wait to ensure that the button is present and clickable as it might be the case it's not available in DOM right away. Check out How to use Selenium to test web applications using AJAX technology article for more details
Assuming all above I believe you should be able to use below code for clicking the button:
continue_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#title='Continue']")))
continue_button.click()

How to locate element in selenium for href

I am trying to locate the element for this:
<a ng-if="showLink && customer.partnerType == 2 && customer.isDirectCustomer" class="cp_text_link ng-binding ng-scope" ng-href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&orgName=Attunix" href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&orgName=ABC">
ABC
</a>
I tried the XPath and CssSelector but its unable to locate element.
Can someone pls help me locate the element
TIA
Simply use text:
driver.findElement(By.xpath("//a[contains(text(),'abc')]"));
It is quite hard to come up with an exact locator without seeing the full code of the page, you're trying to automate.
From what I can see so far it makes sense to stick to ABC text so try the following:
Partial Link Text
driver.findElement(By.partialLinkText("ABC"));
Or the equivalent XPath
driver.findElement(By.xpath("//a[contains(text(),'ABC')]"));
Try using class
driver.findElement(By.cssSelector("a.cp_text_link"))
The desired element is an Angular element so to locate and invoke click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Java based Locator Strategies:
partialLinkText:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("ABC"))).click();
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.cp_text_link.ng-binding.ng-scope[ng-href*='orgId'][href$='ABC']"))).click();
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='cp_text_link ng-binding ng-scope' and contains(#ng-href, 'orgId')][contains(#href, 'ABC')]"))).click();

Check box is not checked using xpath, it opens link

Please see attached screen shots
using xpath "//div[#id='divTermsNdConditions']/div/div/div/label" it clicks on the label and open term and condition link
If use input id 'IsAgreeToNorms' then element is not visible/reachable.
What will be the solution to click on check box and checked?
You can try to click it with Explicit Wait,
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#data-val-required="Your String Value"]")));
You can use any attribute value if its unique,
//input[#data-val-required="Your attribute Value"]
//input[#name="Your attribute Value"]
You can also use, ExpectedConditions.elementToBeClickable

strange Selenium2 No Such Element error

I have this HTML code.
`<div> <i class="icon-plus icon-white"></i>
<span>
<input type="file" multiple="multiple" name="uploadFile" value="Attach2" id="1309261001000145" style="visibility:hidden;position:absolute;top:0;left:0"> <input type="button" onclick="fireFileButton('1309261001000145')" value="Attach" name="input" class="btn_grn btn_sm">
</span>
</div>`
the div is in a normal nested divs. The second is the displayed 'attach file', once clicked, the "fireFileButton" function will click the first form(which is hidden on top left 0,0 position). then a pop up window appear as the type is 'file' to select file to upload and an ajax to upload.
I'm to use Selenium2 to simulate the file upload process.
I use the following codes:
WebElement attach = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("//input[name^='input']")));
attach.click();
WebElement upload = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.id("1309261001000145")));
upload.sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
I also tried directly use sendKeys Function on first element. But have the error of NO SUCH ELEMENT both for first and second . I have used By.id,name,cssSelector,xpath but to no avail.
the elements can be selected using javascript.
Any Suggestion is much appreciated.
By.cssSelector("//input[name^='input']")
Selectors should not contain //,so replace the above with :
By.cssSelector("input[name='input']")
You can refer this to know more about Selectors.
First check : ID tag.
It seems that ID for the input buttons are dynamic. So we cant access through the ID's.
Second check : NAME tag.
We can use, since it has a name for it.
WebElement attach = driver.findElement(By.Name("input"));
attach.click();
If it is not clicking, use Actions.
Actions action = new Actions(driver);
action.moveToElement(attach).click().perform();
You cannot pass the values directly using sendkeys for file upload.
Check this page for my solution in order to access with file upload : How to handle Windows file browse window using selenium webdriver

Resources