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

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.

Related

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

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

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

No such element Exception. class name with double__

I am trying to get the text under the tag. That is Arduus, Gstaad, Switzerland. I tried with the classname and also with xpath.
driver.findElement(By.className("chalet-details__title"));
String chaletTitle = driver.findElement(By.xpath("//div/h1[#class='chalet-details_title']")).getText();
But its giving NoSuchElementException. The classname has double underscore(__) .Is it because of that not working? Can anyone help me with this?
<div class="col-md-12 col-sm-12 col-xs-12">
<h1 class="chalet-details__title">
<span class="chalet-details__title-property">Arduus</span>,
<a class="chalet-details__title-resort" href="/ski- resorts/switzerland/gstaad">Gstaad</a>,
<a class="chalet-details__title-country" href="/ski- resorts/switzerland">Switzerland</a>
</h1>
<div class="chalet-details__rating">
<div class="chalet-details__wrapper">
<span class="chalet-details__star" style="width: 108px;"></span>
<span class="chalet-details__mask"></span>
</div>
</div>
</div>
You can try something like:
driver.findElement(by.cssSelector("h1.chalet-details__title"))
I just tried this and it worked fine with the provided html, if this still fails for you, can you verify that there aren't any iframes on the page.
Both your locators are OK, but you might need to wait for your element to be present in DOM:
WebDriverWait wait= new WebDriverWait(driver,20 );
String chaletTitle = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("chalet-details__title"))).getText();
Another reason for NoSuchElementException: your element could be located inside an iframe, so you need to switch to that frame before handling target element:
driver.switchTo().frame("iframeNameOrId");
If it has no attributes as id or name:
WebElement someFrame = driver.findElement(By.xpath("//iframe[#someAttr='someValue']"));
driver.switchTo().frame(someFrame);
To get all the text under h1 tag find all elements using locator as follows:
List<WebElement> items = driver.findElements(By.cssSelector("h1.chalet-details__title > *")); //it will find all elements immediately under the h1 tag
Now, iterate over the elements.
Complete code:
List<WebElement> items = driver.findElements(By.cssSelector("h1.chalet-details__title > *"));
for(WebElement item : items){
System.out.println(item.getText());
}

using selenium find the existence of element having multiple classes

I have following html element.
using selenium i need to find the existence of the span class my-icon .
Also findout the first div class is 'active'.Since the class contains multiple classes i was not able to find element by class.
<div class="inner my active">
<div class="left-side">
<span class="icon my-icon"></span></div>
<div class="right-side">
<span class="icon-connected"></span>
<button class="button manage">Manage Connection</button>
</div>
</div>
1)Code used to find the existence of span class <span class="icon my-icon"></span> it is not working and getting element is not visible.
WebElement ispresnet = driver.findElement(By.xpath("//span[contains(#class, 'my-icon')]"));
boolean os = ispresnet.isDisplayed();
You should probably wait for element to become visible:
WebDriverWait wait = new WebDriverWait(webDriver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.active span.icon")));
Note that I'm using div.active span.icon CSS selector here which would match the span element having icon class inside a div element having active class. Either the way I wrote the selector, or the explicit wait should help here.