Webdriver - Unable to locate element (Java) - java

I'm new in Selenium and WebDriver.
I have this HTML:
<input id="undefined-undefined-Jobsubject-5546" type="text" value="" data-test="testing-job-subject" style="padding: 0px; position: relative; width: 100%; border: medium none; outline: medium none; background-color: transparent; color: rgb(255, 255, 255); cursor: initial; font: inherit; height: 100%; box-sizing: border-box; margin-top: 14px;"/>
and I have this code:
driver.findElement(By.xpath("//input[#data-test='testing-job-subject']"));
but the error is:
org.openqa.selenium.NoSuchElementException: Unable to locate element: //input[#data-test='testing-job-subject']
I tried also this:
driver.findElement(By.xpath("//*[starts-with(#id,'undefined-undefined-Jobsubject')]"));
because the number in id is generated, so I can't take the By.id(....), but the same error.
And yes,I have in the code the timeouts,so the element is on the page.
Where is the problem? Thanks

If you are getting NoSuchElementException as your provided exception, There may be following reasons :-
May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();

Try this:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[data-test='testing-job-subject']")));

This should work for you.
driver.findElement(By.cssSelector("id*='Jobsubject'"));

Related

element is not clickable at point(987, 466)

#FindBy(xpath="//input[#name='MiddleName']")
private WebElement middlename;
WebDriverWait clickableWait = new WebDriverWait(getWebDriver(), 120);
clickableWait.until(ExpectedConditions.elementToBeClickable(middlename));
middlename.click();
middlename.clear();
waitPlease(2);
middlename.sendKeys("Jhon");
i am using this code to click a input box but i get an error
Element <input type="text" value="as" name="MiddleName"> is not clickable at point (987, 466). Other element would receive the click: <div _ngcontent-c8="" class="modal-body" style="padding: 50px; max-height: 500px; overflow-y: scroll;">...</div>
the block element not in my webpage so whya thats error coming..
i am using testNG Framework in my project:-
any help for this ,Thanks in advance

Selecting from div class dropdown - Selenium but its not working

I am trying to select an option from a drop-down that does not populate until the locator has been clicked. This my solution but it's not working.
List<WebElement> options = driver.findElements(By.cssSelector("mat-select"));
for (WebElement option : options) {
if (option.getAttribute("ng-reflect-value").contentEquals("50757")) {
Actions build = new Actions(driver);
build.moveToElement(option).click().build().perform();
}
}
HTML of the dropdown.
<div role="listbox"
tabindex="-1"
class="ng-tns-c114-22 ng-trigger ng-trigger-transformPanel mat-select-panel mat-primary"
id="mat-select-0-panel"
aria-multiselectable="false"
aria-labelledby="mat-form-field-label-27"
style="transform-origin: 50% 22px 0px; font-size: 15px; opacity: 1; min-width: calc(100% + 32px); transform: scaleY(1);">
<mat-option
_ngcontent-tqo-c274=""
role="option"
class="mat-option mat-focus-indicator mat-active ng-tns-c114-22 ng-star-inserted"
ng-reflect-value="50757"
id="mat-option-0"
tabindex="0"
Please try with the below code: If possible please share the application URL then I can replicate it from my side.
WebElement option = driver.findElement(By.xpath("//mat-
option[contains(text(),'option text')]");
driver.waitUntilElementVisible(option, 10);
driver.findElement(option).click();
Also please refer link for more details on selecting a particular value in a dropdown without using the methods of Select class in Selenium

How to count IMG tags within DIV using Xpath

I want to count how many IMG tags there are under the surface div tag and echo the amount.
<div id="surface" style="width: 4567px; height: 4137px; left: -1850px; top: -1152px; cursor: default;">
<img src="https://media.memories.png" data-seat="L:106|EE:5" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2221px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:6" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2237px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:7" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2253px; top: 1561px; display: block;">
<img src="https://media.memories.png" data-seat="L:106|EE:8" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2269px; top: 1561px; display: block;">
</div>
Here is my non working attempt to get the count however its not returning the count to run my test in selenium IDE.
"//div[#surface='data-seat']/img"
Option 1:
You can simply use the below xpath and then get the count.
xpath to Image - "//div[#id='surface']/img"
System.out.println(driver.findElements(By.xpath("//div[#id='surface']/img")).size());
Option 2:
If you want to make it more complex, use the js to run the query and return the count.
JavascriptExecutor js = (JavascriptExecutor) driver;
Long number_of_imgs = (Long) js.executeScript("return document.querySelectorAll(\"div[id='surface'] img\").length");
System.out.println(number_of_imgs);
I would prefer the option 1.
Try:
//div[#surface='data-seat']/count(img)
For your example html, it outputs 4.
You can use the XPath-1.0 expression
count(//div[#id='surface']/img)
It counts all img children of all div elements.
To print the count of <img> tags within the <div> tag, you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use either of the following Locator Strategies:
xpath:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[#id='surface']//img"))).size());
cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div#surface img"))).size());

remove/change a specific element class attribute value using selenium

I have a problem based on a similar question that has an answer but does not fit for me. I have an element that is not set up like this class="vote-link up" where it would be easy to make the class to change its attribute because that is just one word where mine is not similar to it.
The HTML code looks like this <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"></textarea>
And I want to remove display: none; which are the two last attribute of the HTML code. Or change it to display: shown; performing one of the options I have shown would give the same result that I want I just don't know how to do it.
i.e this is what i desire, to change the HTML to this <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none;"></textarea>
Just for clarification, display is not an attribute it's one of the properties of style attribute. Here is the reference for more information.
Here are the 2 options to handle your scenario.
Option 1: Update the style display property value to block
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement ele = driver.findElement(By.id("g-recaptcha-response"));
System.out.println("Before updating the property:" + ele.getAttribute("style"));
js.executeScript("arguments[0].style.display = 'block';",ele);
System.out.println("After updating the property:" + ele.getAttribute("style"));
Option 2: Remove the style > display property
JavascriptExecutor js = (JavascriptExecutor)driver;
ele = driver.findElement(By.id("g-recaptcha-response"));
js.executeScript("arguments[0].style.removeProperty('display')",ele);
System.out.println("After deleting the property:" + ele.getAttribute("style"));

How to open a WebElement from a Hoover Menu Selenium JAVA

Hello I'm new using selenium and I was trying to execute some tests from a web page.
This is my code:
System.setProperty("webdriver.gecko.driver","C:\\DRIVERS\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//Open Portal Fiscal
driver.get("http://150.23.110.111/Retenciones/");
//Find what field and enter the user and password
driver.findElement(By.id("frmLogin:txtUsr")).sendKeys("arrubio");
driver.findElement(By.id("frmLogin:txtPwd")).sendKeys("gnp00gnp");
driver.findElement(By.id("frmLogin:butLogin")).click();
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath(""));
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("")));
I can enter to the page without problem and I can enter the user and the password to login, but there's a hoover menu on the next page that I can´t use and stops my automatic execution.
This is the xpath and the csspath:
xpath: /html/body/div[3]/div/div/form/div/ul/li[1]/ul/li[1]/a/span
csspath: html body div#content div#leftPanel.ui-layout-unit.ui-widget.ui-widget-content.ui-corner-all.ui-layout-west.blankBck div.ui-layout-unit-content.ui-widget-content form#j_id1833690111_27e067e8.blankBck div#j_id1833690111_27e067e8:j_id1833690111_27e0678e.ui-menu.ui-menubar.ui-widget.ui-widget-content.ui-corner-all.ui-helper-clearfix ul.ui-menu-list.ui-helper-reset li.ui-widget.ui-menuitem.ui-corner-all.ui-menu-parent.ui-menuitem-active ul.ui-widget-content.ui-menu-list.ui-corner-all.ui-helper-clearfix.ui-menu-child.ui-shadow li.ui-menuitem.ui-widget.ui-corner-all a.ui-menuitem-link.ui-corner-all span.ui-menuitem-text
And this is the element that appears inspecting the "Búsqueda" button.
<ul class="ui-widget-content ui-menu-list ui-corner-all ui-helper-clearfix ui-menu-child ui-shadow" role="menu" style="display: block; height: auto; z-index: 1013; left: 0px; top: 28px;">
<li class="ui-menuitem ui-widget ui-corner-all" role="menuitem">
<a class="ui-menuitem-link ui-corner-all" href="/Retenciones/main/faces/m_evaPuntual.xhtml" style="width:120px" tabindex="-1">
<span class="ui-menuitem-text">Búsqueda</span>
</a>
</li>
<li class="ui-menuitem ui-widget ui-corner-all" role="menuitem">
</ul>
How can I select and open the button "Búsqueda" from the hoover menu?
Thanks for the attention :)
try using:
Actions action = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("xpath for menu"));
WebElement item = driver.findElement(by.cssSelector("css selector values for Búsqueda"));
action.moveToElement(menu).moveToElement(item ).click().build().perform();
Try this below code using action class
WebElement menu_element = driver.findElement(By.xpath("your_menu_xpath"));
WebDriverWait wait = new WebDriverWait(driver, 10); //Explicit wait method, wait for web-element till 10 seconds so, your driver should able to find the web-element.
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("Your_submemu_xpath"))));
WebElement sub_menu_element = driver.findElement(By.xpath("Your_submemu_xpath"));
Actions action = new Actions(driver);
action.moveToElement(menu_element).moveToElement(sub_menu_element).click().build().perform();
Explanation:
1) First locate the menu element
2) Provide explicit wait method for few seconds so your driver may able to find the sub_menu_element that you want to go with.
3) After explicit wait locate the sub_menu element, that you want to go with.
4) Using Action class try to move element from menu to sub menu.

Categories

Resources