Selenium html button not clicking - selenium

This is my HTML code
<button type="button" class="button primary" id="submitID" aria-label="Verify and proceed to next step.">
Verify
</button>
This is my selenium code:
WebElement verifyButton = driver.findElement(By.id("submitID"));
verifyButton.click();
The button comes to focus but not clicking.

Try this in case there are multiple elements with the same id:
WebElement verifyButton = driver.findElement(By.xpath(".//button[#id='submitID']"));

You can use javascript executor . Please ref below code
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('submitID').click();");

You can use any of this thing here
Using ID
WebElement verifyBtn = driver.findElement(By.id("submitID"));
verifyBtn.click();
Using class name
WebElement verifyBtn = driver.findElement(By.className("button primary"));
verifyBtn.click()
Using xpath
WebElement verifyBtn = driver.findElement(By.xpath("//button[#aria-label='Verify and proceed to next step.' and #id='submitID']"));
verifyBtn.click()
Using javascriptExecutor
WebElement verifyBtn = driver.findElement(By.id("submitID"));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", verifyBtn );

If ID is unique, you might wanna wait for some time and then click on it.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submitID"))).click();
OR cssSelector :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[type='button'][id='submitID']"))).click();
OR XPATH :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Verify']"))).click();
OR XPATH :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#aria-label,'Verify and proceed to next step.')]"))).click();

what is the output of this? note - findElementS
driver.findElements(By.id("submitID")).size()
for sure there are many buttons with same id, which must not happen in valid html

Related

The radio button is not getting selected with this HTML code for that. How to do that?

<input id="radio2" name="radioGroup" type="radio">
<label class="flRight" for="radio2">
::before
"Senior Citizen"
::after
</label>
WebElement senior = driver.findElement(By.id("radio2"));
senior.click();
Now the problem is the code is not able to click the desired element.
You need to induce WebdriverWait And to click on the radio button use JavaScript Executor since neither webdriver click nor action class is working
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement item=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#class='flRight' and #for='radio2']")));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", item);
Try with this:
WebElement senior = driver.findElement(By.xpath(".//div[#class='radioBtn']/label[2]"));
WebElement close = driver.findElement(By.xpath(".//div[#id='nvpush_cross']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(close));
close.click();
senior.click();
Code didnt worked before because there is popup you need to close it.
Use WebDriverWait:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.id("radio2")));
senior.click();
After seeing the WebSite it looks like the input is not the element you want to click rather the label...
So just change the senior var to:
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='RadioButton']//label[#for='radio2']")));
The best way to automate human actions in the case where the element is not clickable dew to other elements covering them is to use Actions:
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='RadioButton']//label[#for='radio2']")));
Actions actions = new Actions(driver);
actions.moveToElement(senior).click().build().perform();
(using JavascriptExecutor and executeScript doesn't realy click... it just invokes the method that can be good but not for testing...)
As a last resort use JavascriptExecutor:
JavascriptExecutor jse= (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", senior);
Please try below solution :
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#class='flRight' and #for='radio2']")));
Actions action=new Actions(driver);
action.moveToElement(element).click().perform();

Cannot read property 'defaultView' of undefined

<a href="/lightning/n/******__Country" title="Country" tabindex="0" draggable="false" aria-describedby="operationId-17" class="slds-context-bar__label-action dndItem">
<span class="slds-truncate">Country</span></a>
I got the xpath as
WebElement tabName = driver.findElement(By.xpath("//a[contains(#href,'Country')]"));
I need to click the Country link
I have tried the following options but none work
1) driver.findElement(By.xpath("//a[contains(#href,'Country') and #title='Country']")).click();
2) Actions actions = new Actions((WebDriver) driver.getWebDriver());
actions.moveToElement(tabName).click().perform();
3) ((JavascriptExecutor) driver).executeScript("arguments[0].click();", tabName);
waitForSeconds(5);
I am getting invocation of target exception
org.openqa.selenium.WebDriverException: javascript error: Cannot read
property 'defaultView' of undefined
Can any one please tell me how to click the href link?
Try click on its parent/ancestor instead. For example, if you had
//a[text()='link of your text']
and you get the javascript error, try:
//a[text()='link of your text']/parent::*
Try the following code - it works against Salesforce Lightning UI screens:
WebElement element = driver.findElement(By.xpath("your xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Sharing it after testing it and found to be working.
To click on the following link use WebDriverWait and elementToBeClickable and then click on the link using the following xpath.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#title='Country']/span[#class='slds-truncate'][contains(.,'Country')]"))).click();
For those trying out in python, here is how you do it:
>>> button_link = driver.find_element_by_xpath('''//*[#id="tab-23"]/slot''')
>>> driver.execute_script("arguments[0].click();", button_link )
Try JavaScript. It worked for me
WebElement element = driver.findElement(By.xpath("strPath"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

Selenium how to click/ access an a tag with a href that has a javascript function

I have searched but havent found something similar to what Im trying to do. I'm using java by the way, I'm trying to click/access an a tag with selenium. The issue is that I'm not sure how to go about it. There seems to be a function/event that I need to set off but not quite sure how to. I tried a few ways as get text and clicking but I knew that wasn't going to work. Also I seen there are ways of using JavascriptExecutor but not sure how to use it for my case. I will post the tag below and alsothe function signature, that might help. If theres a similar question please post the link.
<a name="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" id="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" ptlinktgt="pt_peoplecode" tabindex="203" onclick="javascript:cancelBubble(event);" href="javascript:submitAction_win0(document.win0,'DERIVED_SSS_SCL_SSS_ENRL_CART$276$');" class="SSSHYPERLINKBOLDSMALL">Enrollment Shopping Cart</a>
the signature
function submitAction_win0(form, id, event)
You don't need any JS. Just use this xpath:
"//a[contains(#onclick,'javascript:cancelBubble(event);')]"
Be sure the element is clickable, see
import org.openqa.selenium.support.ui.ExpectedConditions;
for the case of more matches:
List<WebElement> elements = driver.findElements(By.xpath("//a[contains(#onclick,'javascript:cancelBubble(event);')]"));
int elementIndex = 0; // 0 to get first of the 33 mathes, 32 to get the last one
WebElement element = elements.get(elementIndex);
element.click();
EDIT:
You should use WebDriverWait to avoid NoSuchElementException this way the driver will wait until the element is clickable... it will wait up to 10 seconds you can tell it to wait more if needed...
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Student Center")));
element.click();
Using click():
WebElement element = driver.findElement(By.cssSelector("a[class='SSSHYPERLINKBOLDSMALL']"));
element.click();
Using JavascriptExecutor (Not recommended):
WebElement element = driver.findElement(By.cssSelector("a[class='SSSHYPERLINKBOLDSMALL']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
You can use other locators too... like linkText:
WebElement element = driver.findElement(By.linkText("Enrollment Shopping Cart"));
Or partialLinkText:
WebElement element = driver.findElement(By.partialLinkText("Shopping Cart"));

NoSuchElementException: Unable to locate element uisng selenium web-driver

Here i am trying to click a button using css selector find method.But its unable to locate the element. So i tried giving the time also but still its unable to click the element
Code sample -->
WebDriverWait waitr = new WebDriverWait(driver, 10);
WebElement element = waitr.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#userTable > tbody > tr:nth-child(3) > td.sorting_1 > div > a")));
element.click();
My HTML body Looks as follows
<a data-toggle="dropdown" class="mainAnchor" aria-expanded="false">CA05
<span class="caret" style="margin-left:5px;"><span> </span></span></a>
If i use Thread.sleep(1000); instead of wait it works fine but i don't want to use Thread.sleep.Please help me to solve this issue
To click on the element with text as CA05 you can use the following line of code :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.mainAnchor[data-toggle=dropdown]"))).click();
Hi you can use a fluentwait like the following below.
static Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(elementWaitTime, SECONDS)
.pollingEvery(2,SECONDS)
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver aDriver) {
driver= aDriver;
element.click();
return aDriver.findElement(cssSelector("#userTab a.mainAnchor"));
}
});

Selenium driver find element not working

Selenium web driver finds element unable to find input field on the web page I tried this each and every option XPath, by CSS, by name. the testing site on the local environment.
HTML:
<input class="form-control ng-pristine ng-untouched ng-valid ng-empty" ng-model="email" placeholder="Username" aria-describedby="username" type="text">
xpath:
/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input
css selector:
html.no-js.ng-scope body.pace-done.full-width div#wrapper
div.page-wrapper.white-bg.ng-scope
div.wrapper.wrapper-content.ng-scope div.login-bg.ng-scope
div.container div.row
div.col-sm-6.col-sm-offset-3.col-md-4.col-md-offset-4 div.login-form
form.ng-pristine.ng-valid div.col-sm-12.plr10px div.form-group
div.input-group
input.form-control.ng-pristine.ng-untouched.ng-valid.ng-empty
driver.findElement(By.name("username"))
Here is the Answer to your Question:
As per the HTML you provided, you can use the following xpath to identify the element-
WebElement element = driver.findElement(By.xpath("//input[#ng-model='email' and #placeholder='Username']"));
Incase you are facing an ElementNotVisible exception you can induce ExplicitWait to wait for the element to be clickable as follows:
WebDriverWait wait7 = new WebDriverWait(driver, 10);
WebElement element7 = wait7.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#ng-model='email' and #placeholder='Username']")));
element7.click();
Let me know if this Answers your Question.
Try this, this should work.
WebElement emailInput = new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[ng-model='email']")));
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement Username = driver.findElement(By.xpath("//*[#placeholder='Username' and #type='text'"]));
je.executeScript("arguments[0].scrollIntoView(true);",Username);
Username.sendKeys("Name");