How to locate an element with div format in Selenium. getting error Unable to locate an element with the xpath expression - selenium

Below is the code :
<div class="footer-bottom-left">
<div class="campaignUser">Campaign User</div>
<div class="callLogLookUp">Call Log Look Up</div>
</div>
I have tried the below code in selenium:
driver.findElement(By.xpath(".//div[#class='footer-bottom-left'].//div[#class='callLogLookUp']")).click();`

You missed dash in class name. Also you need to remove second dot (first is also not required). So instead of
.//div[#class='footer-bottomleft'].//div[#class='callLogLookUp']
try
//div[#class='footer-bottom-left']//div[#class='callLogLookUp']
You might also need to implement ExplicitWait:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='footer-bottom-left']//div[#class='callLogLookUp']"))).click();

Related

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="card-id-oidc-i"]/a"}

I'm having this strange error (sometimes works sometimes does not):
no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="card-id-oidc-i"]/a"}
My button has this:
<a href="/auth/realms/Global-Realm/broker/id-oidc-i/login?client_id=web&tab_id=Doz54nelUC0&session_code=gwAePmGfpQ2hBLommJO7Rswc1gNkB90Ctc4">
<div style="width:100%;height: 40px;">
<span class="arrow arrow-bar is-right"></span>
</div>
<div class="image" style="background-repeat: no-repeat;margin:auto; width:115px;height:120px"></div>
<div style="margin-top: 10px;min-width:170px">
<h4 style="text-align:center;"><b>log in</b></h4>
</div>
</a>
It's XPATH is:
//*[#id="card-id-oidc-i"]/a
I did this:
driver.findElement(By.xpath("//*[#id=\"card-id-oidc-i\"]/a")).click();
It is strange because sometimes works just fine but sometimes it fails.
Why?
You probably missing a delay.
Try using this:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='card-id-oidc-i']/a"))).click();
BTW your locator is based on some parent element with id = card-id-oidc-i while you shared here only the child a element HTML.
Also, no need to put \ before " inside a String. You can simply use ' instead as I do here.
NoSuchElementException error may occur when :
HTML element may not be present in a DOM yet. So you have to implement WebDriverWait to wait until element is present and visible in a web page.
HTML element may not be inside frame or iframe.
Maybe in your case it is not in the DOM yet, try to wait until it is visible and on clickable position.

How to construct an XPath which remove text from <br> tag?

I am trying to fetch text before the <br> tag using XPath & Java. I tried multiple solutions, but had no luck yet.
//div[#class="help-block"]/p[count(preceding-sibling::br) < 1]
This is my HTML code:
<div class="help-block">
<p>
This is sample text
<br>
Text after a breakpoint
</p>
</div>
Expected outcome: This is sample text.
Actual outcome: This is sample text Text after a breakpoint
To get the This is sample text Induce WebDiverWait And visibilityOfElementLocated() And get the element.
Induce JavascriptExecutor And get the textContent of firstChild
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='help-block']/p")));
JavascriptExecutor js = (JavascriptExecutor) driver;
System.out.println(js.executeScript("return arguments[0].firstChild.textContent;",element));
I think you can just run this:
driver.find_element_by_xpath("//p[br]").text
This will get the <p> element, but the query also checks to make sure that <p> contains a <br> element inside it. So you are just getting the <p>, and .text should get the text from the <p>.
Your expression was close. But you didn't take into account that p is only one element and you want subnodes of p. So change your expression to the following (also escaping the < char)
//div[#class="help-block"]/p/node()[count(preceding-sibling::br) < 1]
It's output is:
This is sample text

Why do I get a timeout although webelement is visible selenium?

In selenium I have the following code
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#placeholder='Create new collab']")))
in order to select an input field inside the following construct:
<div class="md-input-container md-theme-default md-input-placeholder">
<label>Collab Name</label>
<input placeholder="Create new collab" class="md-input" type="text">
<!---->
<!---->
<!---->
::after
</div>
But I get a timeout exception after a wait for 10 seconds. Manually I am able to click and type something in that input field a second after is is being loaded.
The following ExpectedConditions are unable to find the element:
visibility_of_element_located
element_to_be_clickable
while this methid is able to find the element:
presence_of_element_located
but I am not able to use send_keys to the input field. I get an ElementNotInteractableException error. Also 'clicking' in the element before does not work - same error.
So what to try else?
The div with class - 'md-tabs-wrapper' has a style attribute - 'transform: translate3d(-748px, 0px, 0px);'. This div contains the input box that you are trying to interact. What the transform:translate3d does it is shift the divs and contents to the left and outside the viewport of the browser. You can test it by copying the relevant div in browser and then switching off this style or changing the values.
This kind off explains why the "presence" EC works but visibility and click EC do not. Apparently selenium is not able to figure that element is visible and thus throws the ElementNotInteractableException.
Use ActionChains instead. Use the element from the presence EC.
actions = ActionChains(driver)
actions.move_to_element(element)
actions.send_keys("hello world")
actions.perform()
Also need to clear the existing placeholder text for the sendkeys to work properly.
Try the following:
elem = driver.find_elements_by_xpath("//input[#class='md-input' and #type='text']")
driver.execute_script("arguments[0].scrollIntoView();", elem)
wait = WebDriverWait(driver, 10)
my_element = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#class='md-input' and #type='text']")))
my_element.click()

how to get element inside custom tag in chrome web driver

This is my sample html code.
<div class="content">
<M class="mclass">
<section id="sideA">
<div id="mainContent">
<div class="requestClass">
<span>Check</span>
<input type="text" id="box">
</div>
</div>
<section>
<section id="sideB">
...
<section>
</M>
</div>
I want to set some value to my text field ("box"). So I tired to set like below code
driver.findElement(By.xpath("...")).sendKeys("SetValue");
My Xpath id is correct, it's exist in the page but am getting this error
no such element: Unable to locate element: {"method":"xpath","selector":"id("..."}
Why I am getting this error because of my custom tag,if yes how to get element inside custom tag?
As per the HTML you have provided to fill in some value to the text field represented by <input type="text" id="box"> you can use either of the following line of code:
cssSelector :
driver.findElement(By.cssSelector("section#sideA input#box")).sendKeys("SetValue");
xpath :
driver.findElement(By.xpath("//section[#id='sideA']//input[#id='box']")).sendKeys("SetValue");
If you still want to use XPath. This worked for me-
driver.FindElement(By.XPath(#"//*[#id='box']")).SendKeys("AB‌​");
I don't think the custom tag causes any problem as the CssSelector also works-
driver.FindElement(By.CssSelector(#"m[class='mclass'] input")).SendKeys("AB");
You can use ID or xpath to locate it, My suggestion you have to use ID. Also use Explicit wait till element to be visible.
Using ID, your code is like this:
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
elem.sendKeys("test");
You can also use JavascriptExecutor
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='test';", elem);

Xpath not working as expected while running the test

I am trying to automate the browser, while I try to locate the element via xpath in browser in static mode it is able to highlight the element where as when I run the script it comes back with an error that it is unable to find the element.
xpath I have written:
driver.findElement(By.xpath("//input[#value='soa_b_pbtv_l0_trnkni']/following-sibling::td[1]/child::select[#name='jobaction']")));
Here is the HTML:
<form name="f2" onsubmit="return verify();" action="/ATS/cgi-bin/barcap_jobaction.pl" method="post">
<>
<input name="jobname" type="hidden" value="soa_b_pbtv_l0_trnkni"/>
<input name="jobinstance" type="hidden" value="D03"/>
<input name="jobproceed" type="hidden" value="web"/>
<td style="background-color: #ffffff;">
<select name="jobaction">
if you're trying to select the select, jobaction then try this:
use css selector for the select select[name='jobaction']
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[name='f2']")));
List<WebElement> eleList = driver.findElements(By.cssSelector("form[name='f2']")));
for(WebElement element: eleList) {
if(element.findElement(By.cssSelector("input[name='jobname']")).getText().equalsIgnoringCase("expectedValue")) {
WebElement element = element.findElement(By.cssSelector("select[name='jobaction']"));
}
}
The INPUT is hidden so it won't be found using typical Selenium means. Selenium was designed to only interact with elements that a user can see and interact with. You are able to locate it in the browser because you are using JS or JQuery and they are not designed to ignore hidden elements. One way to get around this is to use JavascriptExecutor... it basically allows you to run JS in Selenium and find hidden elements. Since it sounds like you already have successful locators, I would suggest you look up some tutorials on JSE and you should be set.
If you run into a new issue while using JSE, come back and post a new question and we can try to help you.