i would like to find my TextField in selenium, but i dont know how (i use sellenium for the first time).
I tried:
driver.findElement(By.id("originTextField"))
or by xPath and cssSelector String generated by chrome in dev tools.
Please help me, i would appreciate explanation.
this is html:
NoSuchElementException
org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.
NoSuchElementException can be thrown in 2 cases as follows :
When using WebDriver.findElement(By by) :
//example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
When using WebElement.findElement(By by) :
//example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :
Constant Field Type Value
SESSION_ID public static final java.lang.String "Session ID"
e.g. (Session info: chrome=63.0.3239.108)
DRIVER_INFO public static final java.lang.String "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
BASE_SUPPORT_URL protected static final java.lang.String "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
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 executeScript() method to scroll the element in to view as follows :
WebElement elem = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("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 :
WebElement element = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
element.sendKeys("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.switchTo().frame("frame_name");
driver.switchTo().frame("frame_id");
driver.switchTo().frame(1); // 1 represents frame index
Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
Reference
You can find Selenium's python client based relevant discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Your code is correct, I suspect the issue caused the page not complete load when you find the element.
Try add a long sleep before find element, if adding sleep worked, change sleep to wait.
Here is the code, It means waiting 10s if the element isn’t present:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "originTextField"))
)
Below code will helps you get it resolved
wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("originTextField")));
or
wait = new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOf(element)).wait(20);
Related
I am new to programming and extremely new to web scraping. I need to scrape a table from a web page, where the table is displayed after a video.
As I said on the title, I tried implicit waits like:
driver.implicitly_wait(40)
...
inputElement = driver.find_element_by_class_name("_td")
(I tried the same with the xpath)
and explicit waits:
wait = WebDriverWait(driver, 40)
wait.until(EC.visibility_of_element_located(By.XPATH("path...")
(tried the same with class name)
This is what I get:
NoSuchElementException: Message: no such element: Unable to locate element
I would REALLY appreciate your help!
There is an iframe we need to switch to so do this prior to looking for your elements.
iframe = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(iframe)
i would like to find my TextField in selenium, but i dont know how (i use sellenium for the first time).
I tried:
driver.findElement(By.id("originTextField"))
or by xPath and cssSelector String generated by chrome in dev tools.
Please help me, i would appreciate explanation.
this is html:
NoSuchElementException
org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.
NoSuchElementException can be thrown in 2 cases as follows :
When using WebDriver.findElement(By by) :
//example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
When using WebElement.findElement(By by) :
//example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :
Constant Field Type Value
SESSION_ID public static final java.lang.String "Session ID"
e.g. (Session info: chrome=63.0.3239.108)
DRIVER_INFO public static final java.lang.String "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
BASE_SUPPORT_URL protected static final java.lang.String "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
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 executeScript() method to scroll the element in to view as follows :
WebElement elem = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("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 :
WebElement element = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
element.sendKeys("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.switchTo().frame("frame_name");
driver.switchTo().frame("frame_id");
driver.switchTo().frame(1); // 1 represents frame index
Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
Reference
You can find Selenium's python client based relevant discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Your code is correct, I suspect the issue caused the page not complete load when you find the element.
Try add a long sleep before find element, if adding sleep worked, change sleep to wait.
Here is the code, It means waiting 10s if the element isn’t present:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "originTextField"))
)
Below code will helps you get it resolved
wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("originTextField")));
or
wait = new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOf(element)).wait(20);
I getting below error - if I use linktext() to locate specifications link
Error :
Unable to find element with link text == Specifications
As per the HTML you have shared incase linktext() shows error as:
Error : Unable to find element with link text == Specifications
As an alternative you may need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Specifications"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td.subtabTxtNsel>a.subtabTxtNsel[tag='a']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[#class='subtabTxtNsel']/a[#class='subtabTxtNsel' and contains(.,'Specifications')]"))).click();
The problem is you are using specifications but it is Specifications.
Just with capital S , it should be working fine.
You can use explicit wait, something like :
new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.linkText("Specifications"))).click();
OR with partial link Text :
new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Specifications"))).click();
If both of them still are not working, then you can try this xpath :
//td[#class='subtabTxtNsel']/a[text()='Specifications' and #tag='a']
Your binding language is not specified, I have provided answer in JAVA.
UPDATE :
An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished
Please refer this link for better understanding.
I am facing an issue where I am unable to locate Element on webpage with any type of locator expect Absolute xpath. Here are details:
URL : https://semantic-ui.com/modules/dropdown.html#selection
Required Element Screen shot:
Manually created Xpath Screen shot( Please note that I am able to recognize Element in web application with manually created xpath but Same xpath is not working in selenium code)
But Same xpath is not working in selenium script.
PLEASE NOTE THAT I AM ABLE TO IDENTIFY SAME OBJECT WITH Absolute xpath
Please help to me to understand reason for this.
Here is code:
public static WebDriver driver;
public static void main(String[] args) {
driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://semantic-ui.com/modules/dropdown.html");
//Selection
driver.findElement(By.xpath("//div[#class='ui selection dropdown upward']")).click();
driver.findElement(By.xpath("//div[#class='menu transition visible']/div[text()='Female']")).click();
System.out.println("Done");
This may be issue with your first x-path. You may try the following code. It may work.
driver.findElement(By.xpath("//div[#class='ui selection dropdown']").click();
driver.findElement(By.xpath("//div[#class='menu transition visible']/div[text()='Male']").click();
You are missing a preceding wildcard in you driver.FindElement XPath.
Instead of driver.findElement(By.xpath("//div..."));, do driver.findElement(By.xpath("//*div..."));.
Currently, what your code is doing is telling the XPath locator to find a first level div (not easily possible, as the first item is almost always the document body), but the wildcard of "*" tells it that it can look for the div at any level.
As an aside, please edit your answer up top with actual code instead of pictures so others with the same problem can find their solution easier.
Longshot:
You are using Chrome to look at the source, but Selenium is using Firefox.
There is a chance that the source is rendered differently between the two browsers. Specifically, your xpath is relying on an exact match of class. I do know that FireFox is notorious for modifying source output.
I haven't done any testing but I would not be surprised if the class is in a different order and it's failing on an exact match.
There are two solutions if that is the case:
Don't do a single exact match (xpath = "") but instead do a mix of contains joined with && (contains ui && contains selection && dropdown)
Don't rely on the output from the console tab. Instead "View Page Source" and view what is actually being sent instead of what is being interpreted by the browser
Find the dropdown container element firstly, then use it to find self descendant, like option etc.
driver.get("https://semantic-ui.com/modules/dropdown.html");
// find dropdown container element
WebElement dropdownWrapper = driver.findElement(
By.xpath("//div[#class='html']/div[input[#name='gender']]"));
// expand drop down options by click on the container element
dropdownWrapper.click();
// or click the down arrow
dropdownWrapper.findElement(By.css('i')).click();
// select option
dropdownWrapper.findElement(By.xpath(".//div[text()='Female']")).click();
To locate the element with text as Gender and select the option Female you can use the following Locator Strategy :
Code Block :
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://semantic-ui.com/modules/dropdown.html#selection");
driver.findElement(By.xpath("//div[#class='another dropdown example']//div[#class='ui dropdown selection']")).click();
driver.findElement(By.xpath("//div[#class='another dropdown example']//div[#class='ui dropdown selection active visible']//div[#class='menu transition visible']//div[#class='item' and contains(.,'Female')]")).click();
System.out.println("Gender Female selected.");
Console Output :
Gender Female selected.
This might be helpful to clearify how selectors are working in general:
.(dot): Dot at starting represents the current node. It tells us that the processing will start from the current node.
. .(double dot): It will select parent of current node. For example, //table/. . will return div element because div is the parent of table element.
‘*’: is used to select all the element nodes descending from the current node. For example:
/table/*: It will select all child elements of a table element.
//*: It will select all elements in the document.
//*[#id = ‘username’]: It will select any element in document which has an attribute named “id” with the specified value “username”.
#: It represents an attribute which selects id, name, className, etc. For example:
#id: It will select all elements that are defined with the id attribute in the document. No matter where it is defined in the document.
//img/#alt: It will select all the img elements that are defined with the #alt attribute.
//td[#*]: It will select all td elements with any attribute.
Here is a link to the article:
https://www.scientecheasy.com/2020/07/selenium-xpath-example.html/
I am not able to click on 'Albums' in Facebook.
The HTML is Albums how to locate the element 'Albums' in selenium web driver.
I tried with using driver.findelement(By.xpath(span[#class="_3sz"]) showing error as element not found
And, the html looks the following:
<span class="_3sz">Albums</span>
If I am understanding your problem correctly then that xpath you mentioned returns more than one elements. Use a text based search which is more easier and specific.
driver.findelement(By.xpath("//*[.='Albums']").click();
And, here . is used to directly point to the parent element. Additional wait might be needed to wait for the element to interact. Also, I am assuming you are trying to click the element.
EDIT
Driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
Driver.Navigate().GoToUrl("http://www.facebook.com");
Driver.Manage().Window.Maximize();
Driver.FindElement(By.CssSelector("#email")).SendKeys("your email");
Driver.FindElement(By.CssSelector("#pass")).SendKeys("your pass");
Driver.FindElement(By.CssSelector("[type='submit'][value='Log In']")).Click();
Driver.FindElement(By.CssSelector(".fbxWelcomeBoxName")).Click();
Driver.FindElement(By.XPath("//*[.='Photos']")).Click();
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[.='Albums']")));
Driver.FindElement(By.XPath("//*[.='Albums']")).Click();
By albumname = By.XPath("//strong[.='2014']"); //this should be your album name. In my case it's 2014
wait.Until(ExpectedConditions.ElementExists(albumname));
Driver.FindElement(albumname).Click();
wait.Until(ExpectedConditions.ElementExists(By.CssSelector(".fbPhotoAlbumHeader.fbPhotoAlbumOptionsPresent [type='file']")));
Driver.FindElement(By.CssSelector(".fbPhotoAlbumHeader.fbPhotoAlbumOptionsPresent [type='file']")).SendKeys(#"D:\Users\Saifur\Desktop\FacebookPicture\150232_585410621540701_1836495431_a.jpg");
wait.Until(ExpectedConditions.ElementExists(By.CssSelector(".pvm.phl.footerBox.uiBoxWhite")));
Driver.SwitchTo().ActiveElement();
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("[name='postPhotosButton']")));
Driver.FindElement(By.CssSelector("[name='postPhotosButton']")).Click();
Notice mine is C#
It always best practice to follow this sequence while selecting elements.
1) ID
2) CSS
3) XPath (This will have some issues with different browsers specially IE)
In this case, considering this is no other span class with same name. It would have work like this "span._3sz". Simple and powerful.