Could anyone please help me to hit the javascript link through selenium code.
This is the inspect code and link name is Applications.
Currently I am using below all the list of code, but still it's not hitting the applications link.
driver.findElement(By.id("I6")).click();
driver.findElement(By.xpath("//img[# src='/ibm/console/images/arrow_collapsed.gif']")).click();
driver.findElement(By.xpath("//span[contains(text(),'Applications']")).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//*[contains(text(),Applications')]"));
js.executeScript("arguments[0].click;", element);
WebElement element1 = driver.findElement(By.xpath("//span[contains(text(),'Applications']"));
element1.click();
driver.findElement(By.xpath("//a[#href='javascript:expandCollapse('6');]")).click();
driver.findElement(By.xpath("//div[#id='I6']/..//a[contains(text(),'Applications')]")).click();
Try to click WebElement using javascript this way.
WebElement element = driver.findElement(By.xpath("//span[contains(text(),'Applications']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
I have try with your this html code:
<img src="/ibm/console/images/arrow_expanded.gif" title="Collapse" alt="Collapse" id="I6" border="0" align="absmiddle"> <img src="/ibm/console/images/arrow_collapsed.gif" title="Expand" alt="Expand" id="I6" border="0" align="absmiddle"> <a style="color:#000000;text-decoration:none;" href="javascript:expandCollapse('6');" title="Applications"><img src="/ibm/console/images/arrow_collapsed.gif" title="Expand" alt="Expand" id="I6" border="0" align="absmiddle"><span dir="ltr">Applications</span></a>
Refer Image for more details:
Correct me if I'm wrong, there is typo in all of your xpath like :
driver.findElement(By.xpath("//span[contains(text(),'Applications']")).click(); \\missed `)` in contains method
If see carefully somewhere ' missing and somewhere () , Ok leave it, try the following xpath and let us know still face same issue
driver.findElement(By.xpath("//a[#title='Applications']")).click();
or
driver.findElement(By.xpath("//a[contains(.,'Applications')]")).click();
Related
<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 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");
I am constantly getting "No Such Element Exception" for "First Name" test box
Below is my code:
public class southwestSignUpSave {
WebDriver oBrw;
#Before
public void loadwebsite (){
oBrw = new FirefoxDriver();
oBrw.manage().window().maximize();
oBrw.get("https://southwest.com");
oBrw.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void signUpAndSave(){
oBrw.findElement(By.partialLinkText("OFFERS")).click();
oBrw.findElement(By.partialLinkText("Sign")).click();
//oBrw.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebDriverWait oWait = new WebDriverWait(oBrw, 30);
oWait.until(ExpectedConditions.presenceOfElementLocated(By.id("FIRST_NAME")));
oBrw.findElement(By.id("FIRST_NAME")).clear();
oBrw.findElement(By.id("FIRST_NAME")).sendKeys("abc");
oBrw.findElement(By.id("LAST_NAME")).clear();
oBrw.findElement(By.id("LAST_NAME")).sendKeys("asd");
oBrw.findElement(By.id("EMAIL")).clear();
oBrw.findElement(By.id("EMAIL")).sendKeys("abc#asd.com");
new Select(oBrw.findElement(By.id("HOME_AIRPORT"))).selectByVisibleText("Akron/Canton, OH - CAK");
oBrw.findElement(By.id("IAN")).click();
}
}
I tried to use id and name.
where am I going wrong. I am new to Selenium WD
U can try finding the element using xpath. For that u need to install the firepath plugin in firefox and then inspect the element using firepath.
oBrw.findElement(By.xpath("copy paste the xpath here")).clear();
I would also recommend loading the driver using System property inside loadwebsite() method.
System.setProperty("webdriver.firefox.driver", "//your driver path");
oBrw=new FirefoxDriver();
if the Sign page opens in a new tab/window then u need to navigate to that tab/window because Selenium by default stays in the opening tab. To navigate u need to add the following lines of code after clicking on "Sign"-
Set<String> s=wd.getWindowHandles();
Iterator<String> it=s.iterator();
it.next();//control goes to 1st default tab
String str=it.next().toString();//control goes to the next tab
oBrw.switchTo().window(str);//driver switches to the new window/tab.
if the element is present inside a frame then also u need to switch to that frame first before finding element inside it. Below is the code-
WebElement web=oBrw.findElement(By.xpath("copy paste your frame xpath"));
oBrw.switchTo.frame(web);
now try to find the element present in the new tab/window.
FIRST NAME input text field is inside iframe. Check the below piece of HTML.
<iframe frameborder="0" src="https://luv.southwest.com/servlet/formlink/f?kOHpjQACAY" onload="scroll(0,0);" verticalscrolling="no" horizontalscrolling="no" scrolling="no" title="Click 'n Save signup form"></iframe>
<html dir="ltr">
<head>
<body>
<p>
<span class="required">*Required</span>
</p>
<div class="clear"></div>
<form id="cnsForm" onsubmit="return validateForm();" action="https://luv.southwest.com/servlet/campaignrespondent" method="POST">
<div class="form_field first_name">
<label for="first_name">
<input id="FIRST_NAME" type="text"=""="" maxlength="25" size="22" name="FIRST_NAME">
</div>
...
Hence selenium is unable to find out the element. Here we need to explicitly switch to iframe as below. Insert below code snippet before you find FIRST_NAME. (You can insert well formatted xpath of iframe. I just grabbed it from firebug.)
WebElement iframeSwitch = oBrw.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div[1]/div/div/div[4]/div/div/div/div[3]/iframe"));
oBrw.switchTo().frame(iframeSwitch);
That Text box is inside an iFrame, so you need to switch to that iFrame first then try findElement method to locate textbox.
oBrw.findElement(By.partialLinkText("OFFERS")).click();
oBrw.findElement(By.partialLinkText("Sign")).click();
oBrw.switchTo().defaultContent();
oBrw.switchTo().frame(0);
WebElement id = oBrw.findElement(By.name("FIRST_NAME"));
id.sendKeys("USERNAME");
Hope this helps.
I am Really stuck for the past two days here. I am trying to click sub menu and when I try to click sub menu I get an errors as like the following
Element not found for the sub menu.
I have tried below code
WebElement element = driver.findElement(By.id("x-menu-el-P46915788081933044__folderbrowser_PJL"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
HTML Code
<li id="x-menu-el-P46915788081933044__folderbrowser_PJL" class="x-menu-list-item">
<a id="P46915788081933044__folderbrowser_PJL" class="x-menu-item" href="javascript:var abc = doNothing()" unselectable="on" hidefocus="true">
<img id="ext-gen926" class="x-menu-item-icon " src="netmarkets/images/import.gif">
<span id="ext-gen927" class="x-menu-item-text">Upload Documents from Compressed File</span>
Instead of using the ID you should probably use the class name.
WebElement element = driver.findElement(By.ClassName("x-menu-list-item"));
or you could try using the css selector
WebElement element = driver.findElement(By.cssSelector("li[class='x-menu-list-item']"));
Since the above return multiple items you could just use to return the exact element that you need:
WebElement element = driver.findElement(By.linkText("Upload Documents from Compressed File"));
1st click on the menu and then try the following statement -
driver.findElement(By.xpath("//li[#class='x-menu-list-item']//span[contains(text(),'Upload Documents from Compressed')])).click();
or directly try this -
driver.findElement(By.xpath("//span[contains(text(),'Upload Documents from Compressed')])).click();
I guess mostly the error is due to the span name spaces in between words, if the above dont work, pls attach a screenshot or give some details of the html code, so we can try more options, all d best.
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()