Selenium -- Click on a blank area of an element - selenium

<td width="14%" height="90" valign="top" id="cell_saturday1" name="06/02/2018" onclick="onClick(this);">
<table width="100%" height="30" id="table_saturday1"></table>
</td>
Is there any way I can click anywhere on td in above code not containing table with the id table_saturday1?

You can use this Xpath to click on td which is clickable :
//td[#id='cell_saturday1' and #name='06/02/2018' and contains(#onclick,'onClick(this);')]

You can try with javascript executor as given below.
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('cell_saturday1').click();");
or
WebElement ele = driver.findElement(By.id("cell_saturday1"));
jse.executeScript("arguments[0].click();",ele);
or Using action class
Actions builder = new Actions(driver);
builder.moveToElement(ele, 0, 0).click().build().perform();

To click anywhere within the <td> except the childnode <table> with id as table_saturday1 you can use the following Locator Strategy
Locator Strategy :
cssSelector:
"td#cell_saturday1:not(#table_saturday1)"
xpath:
"//td[#id='cell_saturday1' and not(#id='table_saturday1')]"

Related

What could be the xpath/Css selector for below HTML tag, How to select a specific radio button using Selenium xpath

What could be the xpath/Css selector for below HTML tag , My question is How to click on a radio button of Bike.
let's Assume below are the dynamic radio button, we have 100 numbers radio buttons are present and here we can not predict the index number of our radio button
HTML:
<table>
<tbody>
<tr><td class="textAlignCenter" id="clientDocTypeSelection"><input class="marginL10" name="clientRadio" type="radio"></td><td id="clientDocTypeDescription"> Bike </td></tr>
<tr><td class="textAlignCenter" id="clientDocTypeSelection"><input class="marginL10" name="clientRadio" type="radio"></td><td id="clientDocTypeDescription"> Car </td></tr>
</tbody>
</table>
Find the td with the text you are looking for like this:
tds = driver.find_elements_by_xpath("//td[#class='textAlignCenter']")
for td in tds:
if td.text == 'Bike':
radio_input = td.find_element_by_xpath(".//input[#type='radio']")
To click() on the radio-button with text as Bike you can use either of the following xpath based Locator Strategies:
Using Java and normalize-space():
driver.findElement(By.xpath("//td[#id='clientDocTypeDescription' and normalize-space()='Bike']//preceding::td[1]/input")).click();
Using Java and contains():
driver.findElement(By.xpath("//td[contains(., 'Bike')]//preceding::td[1]/input")).click();
Ideally to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using Java and normalize-space():
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[#id='clientDocTypeDescription' and normalize-space()='Bike']//preceding::td[1]/input"))).click();
Using Java and contains():
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[contains(., 'Bike')]//preceding::td[1]/input"))).click();

How to fix error in identifying element using xpath for SAP Fiori UI

I'm writing a selenium script for identifying an element and send keys to it for a SAP interface.
This for a SAP Fiori UI application.
Even though the xpath I have identified is unique when i run the code in chrome its not identifying the element.
Below is my code :
public static void main(String[] args) {
// declaration and instantiation of objects/variables
String baseURL = "https://mercury.abc.net/default.aspx";
WebDriver driver;
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\abcde\\Desktop\\Eclipse\\Selenium
Practice\\libs\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
driver = new ChromeDriver(options);
// launch Chrome and direct it to the Base URL
driver.get(baseURL);
WebElement MyTimesheetButton =
driver.findElement(By.id("Tile_WPQ8_8_7"));
MyTimesheetButton.click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*
[#id=\"WD027B\"]")));
WebElement Engagement =
driver.findElement(By.xpath("//[#id=\"WD027B\"]"));
Engagement.sendKeys("test");
//close Chrome
driver.close();
}
I should get expected result as element identified and data keyed in to the element but I'm getting actual result as below :
Cannot locate an element using By.xpath: //*[#id="WD027B"]
HTML:
<iframe role="main" frameborder="0" title="The title of the hosted application within the canvas area: My Timesheet" '="" src="/nwbc/ZEP3_FIN_TM_ENTRY_DYNPRO_MSTR/?sap-client=200&sap-language=EN&sap-nwbc-node=page_collection&sap-nwbc-version_hash=392D742742D08304E969040C8A992A95&sap-theme=zcorbu" id="iFrameId_1550067729776" name="iFrameId_1550067729776" style="display: block; width: 1036px; height: 641px;">Your browser is currently configured not to display inline
<table id="WD027B-r" class="lsTblEdf3Whl lsField--table"> <tbody> <tr> <td class="lsTblEdf3Td urBorderBox" style="vertical-align:top;"><input id="WD027B" ct="CBS" lsdata="{0:'WD027B',5:'FREETEXT',7:'WD027C',14:true,20:40,25:'CLIENT_SERVER_PREFIX',26:'F4LOOKUP',30:true,32:40,34:true,35:'VALUE1'}" class="lsTblEdf3 urBorderBox urEdf2TxtHv" value="" role="combobox" name="WD027B" style="vertical-align:top;"> </td> </tr> </tbody> </table>
</iframe>
Try this XPath It should work.
WebElement Engagement =driver.findElement(By.xpath("//table[#id='WD027B-r']/tbody/tr/td/input[#id='WD027B']"));
Engagement.sendKeys("test");
Please let me know if this work for you.
As the element is within an <iframe> and a dynamic element so you need to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='iFrameId_'][src*='FIN_TM_ENTRY_DYNPRO_MSTR']")));
WebElement Engagement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.lsField--table td.urBorderBox>input.urBorderBox[ct='CBS']")));
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#id, 'iFrameId_')][contains(#src, 'FIN_TM_ENTRY_DYNPRO_MSTR')]")));
WebElement Engagement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[contains(#class, 'lsField--table')]//td[contains(#class, 'urBorderBox')]/input[contains(#class, 'urBorderBox') and #ct='CBS']")));

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");

Selenium cannot click menu-item from bootstrap dropdown

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", tmpElement);
List<WebElement> dd_list = driver.findElementsByXPath("//*[#id='masterNavigation']/ul/li[1]/ul/li");
for (WebElement ele : dd_list)
{
System.out.println("Values " + ele.getAttribute("innerHTML"));
if (ele.getAttribute("innerHTML").contains("Event Dashboard")) {
ele.click();
break;
}
}
}
Hi I am trying to Automate bootstrap drop-down menu. It's visibility is hidden by default.Once you hover mouse on it, its visibility property shows visible.I am able to click on drop-down , but after clicking on drop-down my selenium script is not selecting value from drop-down.
Error: Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: Cannot click on
element
HTML Code Snippet
<a class="ui-button-text-icons" id="ctl00_Sitemap1_HyperLink1" href="javascript:void(void);">
<span style="padding-right: 1.3em;">Dashboards</span>
<span class="ui-button-icon-secondary ui-icon ui-icon-triangle-1-s"></span>
</a>
<ul style="visibility: hidden;">
<li class="first featureGranted">
Classic Dashboard
</li>
</ul>
Few things
You don't need to traverse though all li elements to find your desire element you can do it with Xpath
I have no Idea why you are using JavaScript to click first Element, but unless click method provided by Selenium is not working I would suggest not to use JavaScript Click
Error suggests that element is not visible, it could be due to multiple reasons. You could wait using Explicit wait until element is visible as mentioned below. it might resolve your issue
Code
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
// I have no idea why you are clicking using JavaScript
js.executeScript("arguments[0].click();", tmpElement);
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement eventDashboardMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(text(),'Event Dashboard')]")));
eventDashboardMenu.click();

what will be xpath for image in below mention code

I am trying to finding xpath for image.below is my code.i am getting error unable to locate the element.
driver.findElement(By.xpath("//img[#src='./pics/logo /home.jpg']")).click();
Below mention is my table code. from where i am trying to find xpath for image.
<table cellspacing="0" cellpadding="0" width="600" border="0">
<tbody>
<tr>
<tr>
<td style="vertical-align: top;padding-top:10px;padding-right: 3px;">
<td width="30%" style="vertical-align: top;padding-top:10px;">
<a title="Access to Data (S,g,...)" target="_top" href="./action/updateTabs?tabSet=requestId=1457516682135">
<img border="0" src="./pics/logo/home/EMLogoMini.jpg">
</a>
</td>
I have observed that there is a space in your xpath and the URL is different too..
Use below code:-
driver.findElement(By.xpath("//img[#src='./pics/logo/home/EMLogoMini.jpg']")).click();
Or use cssSelector as below :-
driver.findElement(By.cssSelector("img[src='./pics/logo/home/EMLogoMini.jpg']")).click();
List<WebElement> list=driver.findElements(By.xpath("//img[#src='./pics/logo/home/EMLogoMini.jpg']"));
for(WebElement e : list){
e.click();
}
How to click by different ways:-
If your problem is that the element is scrolled off the screen (and as a result under something like a header bar), you can try scrolling it back into view like this:
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY();
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}
if you need you could also add in a static offset (if for example you have a page header that is 200px high and always displayed):
public static final int HEADER_OFFSET = 200;
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY() - HEADER-OFFSET;
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}
If still not work then use JavascriptExecutor
WebElement element= driver.findElement(By."Your Locator"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Hope it will help you :)
try to remove spaces and be sure that the URL is same...