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)
Related
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 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 trying to locate a link "Taschen" in the below code(<a data-flyout-target="handbags" title="Taschen" href="#">Taschen</a>) using selinium webdriver. Could anyone please guide me
<a data-flyout-target="handbags" title="Taschen" href="#">Taschen</a>
Here are your choices:
By.className
By.cssSelector
By.id
By.linkText
By.name
By.partialLinkText
By.tagName
By.xpath
So assuming that "Taschen" is partial link text
WebElement aLink = driver.findElement(By.partialLinkText("Taschen"));
If you haven't bothered with page loads and proper implicit waits then this might work better
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement aLink = wait.until(
ExpectedConditions.elementToBeClickable(By.partialLinkText("Taschen"))
);
aLink.click();
You are not using java? Update your question to show your code and add a language tag to your question. Your brain isn't a network connected device and we only know what you tell us. :-)
You can also use the XPath as below:-
//a[#title='Taschen']/text()
Code will be as below:-
WebElement tash = driver.findElement(By.xpath("Taschen"));
If you need to get the text then you can use :-
String tash = driver.findElement(By.xpath("Taschen")).getText();
You can also use the contains function in XPath
//a[contains(.,'Taschen')]
Hope it will help you :)
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.
I'm trying to navigate in site: http://startupnationbook.com/startup-map
I want to click on link of Startups, however I'm not able to locate the element.
I tried:
elem = driver.findElement(By.xpath("//a[contains(text(),'Startups')]"));
and
elem = driver.findElement(By.xpath("//*[#id='listtoggle' and contains(text(),'Startups')]"));
In both cases I get: "Unable to locate element" error
What is wrong in my expressions and how can I locate the element to perform a click.
The map is in an iframe, you're probably not telling Selenium to look in there. I would also look for the span tag, and since the id listtoggle is used many times (poor design, making it worthless), just look for contains Startups.
// Also should probably use a wait here, in case the page takes too long to load
chromeDriver.switchTo().frame(chromeDriver.findElement(By.tagName("iframe")));
WebElement elem = chromeDriver.findElement(By.xpath("//span[contains(text(),'Startups')]"));
You can do like below :
WebDriver driver = new FirefoxDriver();
driver.get("http://startupnationbook.com/");
driver.findElement(By.xpath(".//*[#id='main-nav']/li[5]/a")).click();
Hope this will work. I have tested.