Error logging into website with selenium webdriver - python

I know that there are many questions with the same problem but none of them have helped me.
This is my script. I am trying to login into the willhaben website. I am from austria that is why I am using willhaben.at
Python version 3.5.4
Selenium version 3.8.0
PhantomJS version 2.1
#need some of the libraries for later code
from bs4 import BeautifulSoup as soup
import requests
import sys
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
input("If ready press 'enter'")
loginurl = 'https://www.willhaben.at/iad/?islogout=true&logoff.y=10'
chrome_options = Options()
chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(executable_path="C:\\Users\\laure\\Downloads\\chromedriver_win32\\chromedriver.exe", chrome_options=chrome_options, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
USERNAME = 'notgonnagiveit :)'
PASSWORD = 'notgonnagiveit'
driver.get(loginurl)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[#id="username"]'))
)
username = driver.find_element_by_xpath('//*[#id="username"]')
password = driver.find_element_by_xpath('//*[#id="password"]')
password.send_keys(PASSWORD)
username.send_keys(USERNAME)
driver.quit()
Error:
File "D:\OneDrive\Dokumente\Willhaben\Willhaben.py", line 73, in <module>
password.send_keys(PASSWORD)
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 352, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 501, in _execute
return self._parent.execute(command, params)
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=63.0.3239.84)
(Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.16299 x86_64)

Try to use :
username = driver.find_element_by_xpath("//input[#id='username']")
password = driver.find_element_by_xpath("//input[#id='password']")

Related

Selenium and Python - Bot for Instagram Login

I'm trying to create a bot to access instagram, but I don't understand why it's giving an error, here's the code:
from time import sleep
from selenium import webdriver
browser = webdriver.Firefox()
browser.implicitly_wait(5)
browser.get('https://www.instagram.com/')
login_link = browser.find_element_by_xpath("//a[text()='Log in']")
login_link.click()
sleep(2)
username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")
username_input.send_keys("username")
password_input.send_keys("12345678")
login_button = browser.find_element_by_xpath("//button[#type='submit']")
login_button.click()
sleep(5)
browser.close()
and the error on cmd is this:
Traceback (most recent call last):
File "C:\Users\*******\Desktop\bot instagram da net.py", line 9, in <module>
login_link = browser.find_element_by_xpath("//a[text()='Log in']")
File "C:\Users\*******\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\*******\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\*******\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\*******\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //a[text()='Log in']
I TRIED PUT A SLEEP AFTER BROWSER.GET, BUT HAD THE SAME ERROR
The following works for me:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.maximize_window()
driver.get('https://www.instagram.com/')
wait.until(ec.element_to_be_clickable((By.NAME, "username"))).send_keys("username")
el = wait.until(ec.element_to_be_clickable((By.NAME, "password")))
el.send_keys("password")
el.send_keys(Keys.ENTER)
Try having a sleep immediately after the browser.get. I think the page hasn't finished loading when you are trying to access the element.

Element not interactable when trying to access a pop-up with Selenium

I am trying to scrape the following website: https://www.nemlig.com/ but it is not as easy as I was used to, as the page I am trying to scrape things off is not static. What I am trying to do using Selenium is click this:
So that the zipcode pop-up is visible. Then, insert a number and hit enter.
This is my take on it:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome(executable_path=r"C:\Users\user\lib\chromedriver_77.0.3865.40.exe")
browser.get('https://www.nemlig.com/')
elem = browser.find_element_by_xpath("//div[#class='timeslot-statusbutton']")
elem.clear()
elem = browser.find_element_by_xpath("//input[#class='prompt__input ng-pristine ng-valid ng-empty ng-touched']")
elem.send_keys("2300")
elem.send_keys(Keys.RETURN)
But everything after browser.get returns me this error:
Traceback (most recent call last):
File "", line 8, in
elem = browser.find_element_by_xpath("//div[#class='timeslot-statusbutton']").click()
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotInteractableException: element not interactable
(Session info: chrome=77.0.3865.90)
How can I do this properly?
You can try this code :
driver = webdriver.Chrome(executable_path = r'C:/Users/***/Downloads/BrowserDriver/chromedriver_win32/chromedriver.exe')
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("https://www.nemlig.com/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".timeslot-prompt.initial-animation-done")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='tel'][class^='pro']"))).send_keys('ABC')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.prompt__button"))).click()
imports will be :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Error with clicking link (<a> tag, type=button) using selenium in python

I have already asked a question similar to this one but it is different.Still can't understand why the find_elements aren't working like in the selenium documents :(.
Python v3.5.4
Selenium v3.8.0
PhantomJS v2.1
from bs4 import BeautifulSoup as soup
import requests
import sys
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
input("If ready press 'enter'")
loginurl = 'https://www.willhaben.at/iad/?islogout=true&logoff.y=10'
chrome_options = Options()
chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(executable_path="C:\\Users\\laure\\Downloads\\chromedriver_win32\\chromedriver.exe", chrome_options=chrome_options, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
USERNAME = 'notgonnagiveit :)'
PASSWORD = 'notgonnagiveit'
driver.get(loginurl)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[#id="username"]'))
)
username = driver.find_element_by_xpath('//input[#id="username"]')
password = driver.find_element_by_xpath('//input[#id="password"]')
password.send_keys(PASSWORD)
username.send_keys(USERNAME)
for i in range(input("Number of products to reactivate: ")):
driver.get("https://www.willhaben.at/iad/myprofile/adadministration/showlist?page=6")
driver.find_element_by_xpath("//a[#class='btn icon-icon_bearbeiten status-expired' and starts-with(#id,'editAdLink-')]").click()
driver.quit()
I am trying to click a link which has the type=button but the tag is <a>.
Now it worked the very first time but not any more.
This is the error:
Traceback (most recent call last):
File "D:\OneDrive\Dokumente\Willhaben\Willhaben.py", line 85, in <module>
editbutton = driver.find_element_by_xpath("//input[#class='btn icon-icon_bearbeiten status-expired'][#type='button']")
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 368, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 858, in find_element
'value': value})['value']
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#class='btn icon-icon_bearbeiten status-expired'][#type='button']"}
(Session info: chrome=63.0.3239.84)
(Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.16299 x86_64)
html code:
<div class="pull-right edit">
<a id="editAdLink-225585118" rel="nofollow" class="btn icon-icon_bearbeiten status-expired" title="Bearbeiten" onclick="IADLIB.tealium.trackClickEvent({"tmsData":{"user_role_id":"0","environment":"web","user_email":"firstname.lastname%40live.co.uk","user_is_c2c":"true","page_type":"My_Ads","user_id":"29268370","user_name":"firstname","event_name":"my_ads","user_gender":"0"}}, "N", "my_ads_edit")
" href="/iad/myprofile/editad?adId=225585118&page=6" type="button">
</a>
</div>
As per the HTML you have shared to click on the link you can use the following line of code :
driver.find_element_by_xpath("//a[#class='btn icon-icon_bearbeiten status-expired' and starts-with(#id,'editAdLink-')]").click()

Headless Chrome Navigate to Page, click to linked page, then printToPDF

I have to print all HTML pages linked to from a product information page:
http://prod.adv-bio.com/ProductDetail.aspx?ProdNo=1197
I have set up a test script to see if I could do this for just one of the HTML files. I used PyChromeDevTools and Selenium, hoping I could use the printToPDF function in DevTools while using Selenium to click the links I need to click.
Here is the code I have so far:
import requests
import time
import re
import websocket
import PyChromeDevTools
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--remote-debugging-port=9222')
chrome_options.add_argument('--window-size=1200x600')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome('C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python36- 32\\selenium\\webdriver\\chromedriver_win32\\chromedriver.exe', chrome_options = chrome_options)
driver.get("http://prod.adv-bio.com/ProductDetail.aspx?ProdNo=1197")
browser = PyChromeDevTools.ChromeInterface()
browser.Network.enable()
browser.Page.enable()
driver.implicitly_wait(20)
driver.get("http://prod.adv-bio.com/ProductDetail.aspx?ProdNo=1197")
time.sleep(3)
url = driver.find_element_by_partial_link_text("Continuing")
time.sleep(3)
ActionChains(driver).click(url).perform()
time.sleep(3)
Page.printToPDF()
time.sleep(5)
and I am getting this error
Traceback (most recent call last):
File "C:\Users\me\SOtestfile.py", line 29, in <module>
driver = webdriver.Chrome('C:\\Python27\\Lib\\site-
packages\\chromedriver_win32\\chromedriver.exe', chrome_options =
chromeOptions, desired_capabilities=capabilities)
File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: chrome not reachable
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)
I have updated chromedriver to 2.31 and have chrome version 60, should I be using canary? I appreciate any help on this.
from selenium.webdriver.common.desired_capabilities import
DesiredCapabilities
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("headless")
chromeOptions.add_argument("window-size=1366,768")
capabilities = DesiredCapabilities.CHROME.copy()
capabilities["browserName"] = "chrome"
driver = webdriver.Chrome(chrome_options=chromeOptions,
desired_capabilities=capabilities)
Worked here!! (Chrome 60 + Chromedriver 2.31) Hope this helps!!

Can't find element selenium python

<input placeholder="Enter Password" class="password-input" type="password">
Above is the element I am trying to send keys to using the below code.
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')
driver.get('https://howsecureismypassword.net/')
elem = driver.find_element_by_name('password-input')
elem.send_keys('password')
html = driver.page_source
This is the error I get and can't figure out why.
RESTART: C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\Science
Experiment\Science_Experiment_Password_Tester.py
Traceback (most recent call last):
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\Science Experiment\Science_Experiment_Password_Tester.py", line 18, in <module>
elem = driver.find_element_by_name('password-input')
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"password-input"}
(Session info: chrome=57.0.2987.133)
(Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393 x86_64)
This is the site - https://howsecureismypassword.net/
Try find_element_by_class_name instead of find_element_by_name.
Looking at the HTML of your site, the class name of the element you want is password-input, not the name.
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')
driver.get('https://howsecureismypassword.net/')
elem = driver.find_element_by_class_name('password-input')
elem.send_keys('password')
html = driver.page_source

Categories