Set-up
I'm trying to log in to a website using Python + Selenium.
My code to load the website is,
browser = webdriver.Firefox(
executable_path='/mypath/to/geckodriver')
url = 'https://secure6.e-boekhouden.nl/bh/'
browser.get(url)
Problem
Selenium cannot locate the element containing the account and password fields.
For example, for the field 'Gebruikersnaam',
browser.find_element_by_id('txtEmail')
browser.find_element_by_xpath('//*[#name="txtEmail"]')
browser.find_element_by_class_name('INPUTBOX')
all give NoSuchElementException: Unable to locate element.
Even worse, Selenium cannot find the body element on the page,
browser.find_element_by_xpath('/html/body')
gives NoSuchElementException: Unable to locate element: /html/body.
I'm guessing something on the page is either blocking Selenium (maybe the 'secure6' in the url) or is written in a language/form Selenium cannot handle.
Any suggestions?
All elements are inside the frame. So that, it is throwing No Such Element exception. Please try to switch to the frame before all actions as given below.
browser = webdriver.Firefox(
executable_path='/mypath/to/geckodriver')
url = 'https://secure6.e-boekhouden.nl/bh/'
browser.get(url)
browser.switch_to.frame(browser.find_element_by_id("mainframe"))
browser.find_element_by_id('txtEmail')
browser.find_element_by_xpath('//*[#name="txtEmail"]')
browser.find_element_by_class_name('INPUTBOX')
Related
I am new to web scraping and I am trying to scrape reviews off amazon.
After going on a particular product's page on amazon I want to click on the 'see all reviews' button. I did inspect element on the page, I found that the see all reviews button has this structure
structure
So I tried to find this element using the class name a-link-emphasis a-text-bold.
This is the code I wrote
service = webdriver.chrome.service.Service('C:\\coding\\chromedriver.exe')
service.start()
options = webdriver.ChromeOptions()
#options.add_argument('--headless')
options = options.to_capabilities()
driver = webdriver.Remote(service.service_url, options)
driver.get(url)
sleep(5)
driver.find_element_by_class_name('a-link-emphasis a-text-bold').click()
sleep(5)
driver.implicitly_wait(10)
But this returns me the following error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".a-link-emphasis a-text-bold"}
What am I doing wrong here?
driver.find_element_by_class_name('a-link-emphasis.a-text-bold').click()
By class expects single class not multiple but you can use the above syntax , remove space with . as it uses css under the hood, or use :
driver.find_element_by_css_selector('.a-link-emphasis.a-text-bold').click()
driver.find_element_by_css_selector('[class="a-link-emphasis a-text-bold"]').click()
I am trying to make a bot that web scrapes a website. I have got it to the stage in which it puts in the user name and pass word. The website then takes me to a different URL, (from the portal to the home page). The bot cannot seem to find any elements on this new home page. The URL has changed, and I believe that the code is not scraping this new page. How do I update the code, if I simply use the URL of the home page, the website then asks for the username and password again.
I have provided code below that has the same problem, except this time with searching google and then finding the elements as I do not wish to provide personal details about the website I am scraping.
from selenium import webdriver
driver = webdriver.Chrome(r'C:\webdrivers\chromedriver.exe')
driver.get('https://google.com')
driver.implicitly_wait(3)
search_bar = driver.find_element_by_name('q')
search_bar.send_keys("github")
search_bar.submit()
driver.implicitly_wait(3)
element = driver.find_element_by_class_name("LC20lb DKV0Md")
print(element)
driver.implicitly_wait(3)
driver.close()
# If I was to try and find any of the elements on this page using driver.find_element_by_class_name, it would result in the following error message selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="inputfield"]"}
# (Session info: chrome=84.0.4147.105)
driver.close()
Edit:
I am on Windows 10 and using Google chrome
Edit 2:
I believe it may have something to do with the driver scraping the old URL, rather then the new URL that has been searched and contains the search results, is there anyway to perhaps update which URL it is scraping
try to use implicitly_wait instead of sleep
from selenium import webdriver
driver = webdriver.Chrome(r'C:\webdrivers\chromedriver.exe')
driver.get('https://google.com')
driver.implicitly_wait(3)
search_bar = driver.find_element_by_name('q')
search_bar.send_keys("github")
search_bar.submit()
driver.implicitly_wait(3)
driver.close()
I'm using Python and Selenium in PyCharm to go to the SEC website to download a 10-K CSV file. Ideally, the program should ask for user input for a "ticker symbol", then go to the SEC's website, input the ticker symbol provided and download the 10-K and 10-Q CSV files from the page. I was using Microsoft's ticker symbol (MSFT) as an example test. The SEC's Edgar search website is this:
https://www.sec.gov/edgar/searchedgar/companysearch.html
and I am using the 'Fast Search' search engine. I created a function 'get_edgar_results' to perform this download. It might be that I'm new to web scraping, but I thought I identified the HTML tags correctly on where to put my search term. Previous problems suggested that I might need to have the program wait before searching for the HTML element, so I added code for the program to wait. I continue getting this error:
line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="Find"]
My code is below:
import selenium.webdriver.support.ui as ui
from pathlib import Path
import selenium.webdriver as webdriver
ticker_symbol = input("please provide a ticker symbol: ")
def get_edgar_results(ticker_symbol):
url = "https://www.sec.gov/edgar/searchedgar/companysearch.html"
driver = webdriver.Firefox(executable_path=r"C:\Program Files\JetBrains\geckodriver.exe")
wait = ui.WebDriverWait(driver,30)
driver.set_page_load_timeout(30)
driver.get(url)
search_box = driver.find_element_by_id("Find")
search_box.send_keys(ticker_symbol)
search_box.submit()
annual_links = driver.find_elements_by_class_name("10-K")
quarterly_links = driver.find_elements_by_class_name("10-Q")
results = []
driver.close()
driver.quit()
return results
get_edgar_results(ticker_symbol)
Any help would be greatly appreciated.
Consider using a waitUntil or Implicit/Explicit waits method to wait until an element is loaded. This way you can circumvent the error shown above, code below for wait until method.
browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
print "Page is ready!"
except TimeoutException:
print "Loading took too much time!"
You also seem to have an error in the following code
search_box = driver.find_element_by_id("Find")
search_box.send_keys(ticker_symbol)
search_box.submit()
The id=find locates the Search Box and not the input element and therefore sending keys value to a button is incorrect. I would recommend you to use the xpath to uniquely locate the element of your choice.
The following will send a value to the input box and will make a button click on the SEARCH button.
driver.findElement(By.xpath("//*[#id="lesscompany"])).sendKeys("your value");
driver.findElement(By.xpath("//*[#id="search_button_1"]")).click();
# _*_coding:utf-8_*_
from selenium import webdriver
driver=webdriver.Chrome()
url="https://login.alibaba.com"
driver.get(url)
driver.implicitly_wait(3)
print(driver.page_source)
driver.quit()
i want to login by selenium, but can't find the login id "fm-login-id"
To be able to handle elements inside authentication form, you need to switch to iframe:
driver=webdriver.Chrome()
url="https://login.alibaba.com"
driver.get(url)
deiver.switch_to.frame("alibaba-login-box")
driver.find_element_by_id("fm-login-id").send_keys("my email")
driver.find_element_by_id("fm-login-password").send_keys("my password")
Alibaba's login is in different iFrame that's why you are not getting its id and got the error "Element not found"
I created one sample for you in java selenium and it's working.
driver.get("https://passport.alibaba.com/mini_login.htm?lang=en_us&appName=icbu&appEntrance=default&styleType=auto&bizParams=¬LoadSsoView=false¬KeepLogin=true&isMobile=false&ut=B8605a4b80fd5c7e8e8957efccc7ad5ea&showKeepLogin=true&showSnsLogin=true&cssLink=https%3A%2F%2Fu.alicdn.com%2Fcss%2F6v%2Frun%2Fcommon%2Fxman%2Fhavana-thirdpart-login.css®Url=https%3A%2F%2Faccounts.alibaba.com%2Fregister%2Fregister.htm%3Freturn_url%3Dhttp%253A%252F%252Fwww.alibaba.com&rnd=0.2729010632370805");
Thread.sleep(3);
driver.findElement(By.xpath("//*[#id='fm-login-id']")).sendKeys("Test_User");
I ran this code and and it's working fine.
As per the URL you have shared url="https://login.alibaba.com", to locate the Email Address field i.e fm-login-id through Selenium-Java you can use either of the following :
xpath :
driver.find_element_by_xpath("//input[#id='fm-login-id']")
cssSelector :
driver.find_element_by_css_selector("input#fm-login-id")
I'm trying to use Selenium & Python to scrape a website (http://epl.squawka.com/english-premier-league/06-03-2017/west-ham-vs-chelsea/matches). I am using the webdriver to click a heading and then wait for the new information to load before clicking on an object before scraping the resulting data (which loads from the clicking). My problem is that I keep on getting an 'Unable to locate element error.
I've taken a screenshot at this point and can physically see the element and I've also printed the entire source code and can see that the element is there.
driver.find_element_by_id("mc-stat-shot").click()
time.sleep(3)
driver.save_screenshot('test.png')
try:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"svg")))
finally:
driver.find_element_by_xpath("//g[3]/circle").click()
time.sleep(1)
goalSource = driver.page_source
goalBsObj = BeautifulSoup(goalSource, "html.parser")
#print(goalBsObj)
print(goalBsObj.find(id="tt-mins").get_text())
print(goalBsObj.find(id="tt-event").get_text())
print(goalBsObj.find(id="tt-playerA").get_text())
and the result is an error:
"selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //g[3]/circle"