How to find a certain web-element on the web-page - java

Unable to locate the 'Number of instances' field on https://cloud.google.com/products/calculator/ by the customized XPath with WebDriver (Java-powered)
I have tried the xpath: //input[#name = "quantity" and #id="input_52"]
It works fine with Ctrl + F with the Chrome inspect code feature, but not with WebDriver
Here is the stacktrace message in Idea:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#name = "quantity" and #id="input_52"]"}
Thank You for Your kind help in advance

element you trying to operate on is inside frame you have to select frame then you can perform operation on element do the following step to reach to element inside iframe(id of iframe is - id="idIframe") -
driver.switch_to.frame('idIframe');
driver.find_element_by_xpath("//input[#name = 'quantity' and #id='input_52']");

Related

How to find xpath for first searched product

How to find XPath for the first searched element on given page
https://www.amazon.com/b/?encoding=UTF8&node=2346727011&bbn=7141123011&ref_=Oct_d_odnav_1040660&pd_rd_w=WJf7p&pf_rd_p=72459b27-e231-4837-b61c-b057ff0c50ac&pf_rd_r=YMKJ7M0AMXW5GER3MMRQ&pd_rd_r=eba15a0a-f59c-4dfd-a693-7c2f7f3416cf&pd_rd_wg=LRrkE
I tried the below XPath..on html page its showing the element but when putting on selenium code it's showing the error invalid selector: Unable to locate an element with the xpath expression
//div[#id='CardInstance1wPgwM8pHmoJmdnyjYOeWg']//child::div[2]//div[1]//div[1]//div[1]//div[2]//div[#class='a-section a-spacing-none a-spacing-top-small s-title-instructions-style']//span
Please help me to find xpath for first searched product.
Thanks in Advance.
You can directly use
//li[starts-with(#class,'octopus-pc-item')]
this does have 26 entries and is not unique in HTML, however, findElement will return the first matching node.
Furthermore, you can do indexing like below
(//li[starts-with(#class,'octopus-pc-item')])[1]
and [2] for the second product and so on... for other products
Perform click like below
Using ExplicitWaits
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[starts-with(#class,'octopus-pc-item')]"))).click();
Using findElement
Thread.sleep(3000);
driver.findElement(By.xpath("(//li[starts-with(#class,'octopus-pc-item')])[1]")).click();

The error that I am seeing on the console: "NoSuchElementException" when testing out an website using selenium. I'm new at this. unable to key in pwd [duplicate]

I'm trying to play QWOP using Selenium on Chrome but I keep getting the following error:
selenium.common.exceptions.NoSuchElementException:
Message: no such element: Unable to locate element
{"method":"id","selector":"window1"
(Session info: chrome=63.0.3239.108
(Driver info: chromedriver=2.34.522913
(36222509aa6e819815938cbf2709b4849735537c), platform=Linux 4.10.0-42-generic x86_64)
while using the following code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
browser = webdriver.Chrome()
browser.set_window_size(640, 480)
browser.get('http://www.foddy.net/Athletics.html?webgl=true')
browser.implicitly_wait(10)
canvas = browser.find_element_by_id("window1")
canvas.click()
while (True):
action = ActionChains(browser)
action.move_to_element(canvas).perform()
canvas.click()
canvas.send_keys("q")
The same code works perfectly on Firefox, but because I want to use chrome's capability to run an webgl game in headless mode I can't really switch to Firefox.
Any workarounds to get this working?
NoSuchElementException
selenium.common.exceptions.NoSuchElementException popularly known as NoSuchElementException is defined as :
exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None)
NoSuchElementException is basically thrown in 2 cases as follows :
When using :
webdriver.find_element_by_*("expression")
//example : my_element = driver.find_element_by_xpath("xpath_expression")
When using :
element.find_element_by_*("expression")
//example : my_element = element.find_element_by_*("expression")
As per the API Docs just like any other selenium.common.exceptions, NoSuchElementException should contain the following parameters :
msg, screen, stacktrace
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//*[#id='create-portal-popup']/div[4]/div[1]/button[3]"}
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.10240 x86_64)
Reason
The reason for NoSuchElementException can be either of the following :
The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
The WebElement you are trying to locate is within an <iframe> tag.
The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use execute_script() method to scroll the element in to view as follows :
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].scrollIntoView();", elem)
Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through executeScript() method as follows :
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.send_keys("text_to_send")
To check if the element is within an <iframe> traverse up the HTML to locate the respective <iframe> tag and switchTo() the desired iframe through either of the following methods :
driver.switch_to.frame("iframe_name")
driver.switch_to.frame("iframe_id")
driver.switch_to.frame(1) // 1 represents frame index
Here you can find a detailed discussion on How can I select a html element no matter what frame it is in in selenium?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with expected_conditions set to proper method as follows :
To wait for presence_of_element_located :
element = WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "element_xpath']")))
To wait for visibility_of_element_located :
element = WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "element_css")
To wait for element_to_be_clickable :
element = WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "element_link_text")))
This Usecase
You are seeing NoSuchElementException because the id locator doesn't identifies the canvas uniquely. To identify the canvas and click() on it you have to wait for the canvas to be clickable and to achieve that you can use the following code block :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//canvas[#id='window1']"))).click()
Reference
You can find Selenium's java client based relevant discussion in:
NoSuchElementException, Selenium unable to locate element

Java selenium using xpath : NoSuchElementException

Now I'm doing browser test with selenium(java).
However, there are some problems with xpath.
I tried below code.
webDriver.findElement(By.xpath("//button[#onclick='addUserWf();return false;']")).click();
with web element
<button class="btn-etc btn-object-add" onclick="addUserWf();return false;">...</button>
Maybe you can refer, spring boot print :
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[#onclick='addUserWf();return false;']"}
I can see above element(It means element displayed and visible), so I can't understand.
Add : I tried this, but result was same.
webDriver.findElement(By.xpath("//button[#class='btn-etc btn-object-add']")).click();
Someone knows this?
Try below xpath:
webDriver.findElement(By.xpath("//button[#onclick=\"addUserWf();return false;\"]")).click();
or
webDriver.findElement(By.xpath("//button[starts-with(#onclick='addUserWf')]")).click();
And its always good practice to use webdriver wait before clicking on any button.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath(XPATH)))

Selenium test fails with java

I use Selenium with cucumber in Java and when trying to do that :
WebElement MyAccountLink = driver.findElement(By.className("btn-outlined-white_medium_block"));
MyAccountLink.click();
I got this error:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"class name","selector":"btn-outlined-white_medium_block"}
How can i solve it ?
Thanks.
use 'XPath' or 'cssSelector' instead of 'className', because it doesn't matters which locator you are using. you need to find the element and do automation on that element.
WebElement MyAccountLink = driver.findElement(By.xpath("right click on element and copy xpath and paste it here"));
MyAccountLink.click();
Hope XPath will work in any case.

unable to locate href element within a mouse over area - Selenium web driver

I tried to locate a link "Sign in" located in the window which appeared when mouse move on to "Mail" link on Yahoo. I can get the xpath using firebug. but when i used it in the script, it doesn't work.
HTML snippet :
<a id="yui_3_18_0_4_1456816269995_943" class="C($menuLink) Fw(b) Td(n)"
data-ylk="t3:usr;elm:btn;elmt:lgn;" data-action-outcome="lgn"
href="login.yahoo.com/config/…; data-rapid_p="23">Sign in</a>
I tried this in my code within main method,
WebElement element = driver.findElement(By.id("uh-mail-link"));
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();
id selecter;
driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();
It prompts this error;
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"yui_3_18_0_4_1456804882382_929"}
Command duration or timeout: 17 milliseconds
Can we locate it using id of appeared window ".//*[#id='yui_3_18_0_4_1456804882382_919']" and linkText "Sign in", or are there any other methods to locate it in the script.
You're supposed to pass just id to By.id(), not an XPath expression :
driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();
or use By.xpath() instead of By.id() if you need to find the element by XPath expression, for example, by using combination of id and link text to locate the target element.
UPDATE :
You can filter element by its text content and id like so :
//a[#id='yui_3_18_0_4_1456816269995_943' and .='Sign in']
Have you tried putting 2 functions ? 1 for mouse over and 1 for actual clicking ?
I have a sample in Java if you need.
public void mouseOver(String xPath){
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath(xPath));
action.moveToElement(element).moveToElement(driver.findElement(By.xpath(xPath))).click().build().perform();
Thread.sleep(500); //too actualy see if is it performs
}
public void click(String xpath) {
driver.findElement(By.xpath(xpath)).click();
}
You can change the searching method from xpath to id, or whatever you need.
Be aware that the mouseOver function has a different xpath/id to send from the click method because , you mouseOver on first button, and you click the other link that will apear after.
Hope it helps.

Categories

Resources