new to selenium. For some reason ChromeOptions are not working. What am I doing wrong here?
I've tried a few different things I've found here on stackoverflow but nothing is working. It doesn't load the proper profile. Thanks in advance.
import undetected_chromedriver as uc
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
if __name__ == '__main__':
email = "#"
password = "#"
options = webdriver.ChromeOptions()
options.add_argument(r'--users-data-dir=C:\Users\dsfgdfs\AppData\Local\Google\Chrome\User Data\Profile 65')
browser = uc.Chrome(options=options,
)
browser.get('https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&hl=en&flowName=GlifWebSignIn&flowEntry=ServiceLogin')
browser.find_element(By.ID, 'identifierId').send_keys(email)
browser.find_element(
By.CSS_SELECTOR, '#identifierNext > div > button > span').click()
password_selector = "#password > div.aCsJod.oJeWuf > div > div.Xb9hP > input"
WebDriverWait(browser, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, password_selector)))
browser.find_element(
By.CSS_SELECTOR, password_selector).send_keys(password)
browser.find_element(
By.CSS_SELECTOR, '#passwordNext > div > button > span').click()
You have missing import
from selenium.webdriver.chrome.options import Options
Related
I am struggling to keep the session open all the time. The code executes, but only when the link is present.
driver.refresh
doesn't seem to keep the page refreshed.
Is there a way to refresh the page every couple seconds?
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
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
user_name = "username"
password = "password"
driver = webdriver.Chrome()
driver.get('https://mywebsite.com/boardslots.aspx?date=07%2F31%2F2022')
element = driver.find_element(By.ID,"txtUsername")
element.send_keys(user_name)
element = driver.find_element(By.ID,"txtPassword")
element.send_keys(password)
element.send_keys(Keys.RETURN)
while True:
try:
driver.find_element(By.LINK_TEXT,"Claim")
except NoSuchElementException:
driver.refresh
else:
driver.find_element(By.LINK_TEXT,"Claim").click()
try:
# element = WebDriverWait(driver,1)
element= WebDriverWait(driver,
10).until(expected_conditions.visibility_of_element_located((By.XPATH, '/html
/body/form/div[3]/div[3]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[9]/a')))
finally:
driver.find_element(BY.ID,"btnSubmitSingle").click()
#/html/body/form/div[3]/div[3]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[9]/a
I am working on a web scraping project at the moment. The website I am trying to get data from is not the easiest to work with. I am using Selenium, and have worked my way through most of the items I want to select.
The website: http://crashinformationky.org/AdvancedSearch
I can get the website to open, and select different properties. But when I select Data, then try and "click" on Today, to change the date. Nothing works.
I have tried using the xpath, css selectore, link text, partial link text, nothing works.
Here is my most recent attempt.
WebDriverWait(driver, timeout=5).until(driver.find_element(By.CSS_SELECTOR, '#QueryPanel-cond-2 > div:nth-child(4) > a'))
date_one = driver.find_element(By.CSS_SELECTOR, '#QueryPanel-cond-2 > div:nth-child(4) > a')
date_one.click()
date_one_enter = driver.find_element(By.XPATH,'//*[#id="dp1647307835636"]')
date_one_enter.send_keys('01/01/2016')
Welcome Kyle!
As a personal preference I like to use Full XPATH's when selecting items with Selenium. I was able to click on TODAY, clear the field, send a new date and hit ENTER. See below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--enable-logging')
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
url = "http://crashinformationky.org/AdvancedSearch"
driver.get(url)
WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div[3]/form/div[2]/div/div[1]/div[2]/div[2]/div/div[3]/a"))).click()
WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[10]/div/div[3]"))).click()
WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div[3]/form/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/div/div[3]/a"))).click()
field = driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/form/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/div/div[3]/input")
field.send_keys(Keys.COMMAND + "a")
field.send_keys(Keys.DELETE)
field.send_keys('01/01/2016')
field.send_keys(Keys.ENTER)
Do note that I am using a MAC, so when I send the Keys.COMMAND that would be Keys.CONTROL for Windows.
I am getting this error when i am trying to enter username and password automatically on twitter website using Firefox browser:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .session[username_or_email]
The set of code i wrote so far is as follows:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class TwitterBot:
def __init__(self,username,password):
self.username = username
self.password = password
self.bot = webdriver.Firefox()
def login(self):
bot = self.bot
bot.get('https://twitter.com/')
time.sleep(3)
bot.maximize_window()
bot.implicitly_wait(3)
email = bot.find_element_by_class_name('session[username_or_email]')
password = bot.find_element_by_class_name('session[password]')
email.clear()
password.clear()
email.send_keys(self.username)
password.send_keys(self.password)
run = TwitterBot('jok.moe#hotmail.com', '123456')
run.login()
Does anyone have any idea how to fix this ?
The element looks like:
<input class="js-username-field email-input js-initial-focus" type="text" name="session[username_or_email]" autocomplete="on" value="" placeholder="Phone, email or username">
So you could do something like:
email = bot.find_element_by_xpath('//input[#name="session[username_or_email]"]')
Seems you were pretty close. To send a character sequence to the Email and Password fields, you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class TwitterBot:
def __init__(self,username,password):
self.username = username
self.password = password
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
self.bot = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
def login(self):
bot = self.bot
bot.get('https://twitter.com/login')
WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.js-username-field.email-input.js-initial-focus[name='session[username_or_email]']"))).send_keys(self.username)
bot.find_element_by_css_selector("input.js-password-field[name='session[password]']").send_keys(self.password)
run = TwitterBot('jok.moe#hotmail.com', '123456')
run.login()
Using XPATH:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class TwitterBot:
def __init__(self,username,password):
self.username = username
self.password = password
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
self.bot = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
def login(self):
bot = self.bot
bot.get('https://twitter.com/login')
WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='js-username-field email-input js-initial-focus' and #name='session[username_or_email]']"))).send_keys(self.username)
bot.find_element_by_xpath("//input[#class='js-password-field' and #name='session[password]']").send_keys(self.password)
run = TwitterBot('jok.moe#hotmail.com', '123456')
run.login()
Browser Snapshot:
I have the following code:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.common.keys import Keys
if __name__ == '__main__':
path_to_chromedriver = r'C:\chromedriver' # change path as needed
browser = webdriver.Chrome(executable_path=path_to_chromedriver)
wait = WebDriverWait(browser, 10)
browser.get("https://pjm.com/")
wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/form/div[3]/div/div[1]/div/div[1]/span[2]"))).click()
wait.until(EC.presence_of_element_located((By.ID, "IDToken1"))).send_keys("user")
wait.until(EC.presence_of_element_located((By.ID, "IDToken2"))).send_keys("pwd")
But the last two lines of codes are not able to execute and I don't have a clue why it should be like that.
I see that the click() will open a new tab, in which case you have to switch to that tab before proceeding with login details:
browser.switch_to.window(browser.window_handles[1])
wait.until(EC.presence_of_element_located((By.ID, "IDToken1"))).send_keys("user")
wait.until(EC.presence_of_element_located((By.ID, "IDToken2"))).send_keys("pwd")
As my code shows, I am trying to automate the process of logging and other things using selenium in python 3.7. I am struck as it is showing "AttributeError: element_to_be_clickable has no object click" in the line botton_to_click().
from bs4 import BeautifulSoup
from bs4.element import Tag
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
base = 'https://www.wsj.com'
url = 'https://www.wsj.com/search/term.html?KEYWORDS=cybersecurity&min-date=2018/04/01&max-date=2019/03/31&isAdvanced=true&daysback=90d&andor=AND&sort=date-desc&source=wsjarticle,wsjpro&page=1'
browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')
browser.get(url)
browser.find_element_by_id('editions-select').click()
browser.find_element_by_id('na,us').click()
botton_to_click = WebDriverWait(browser, 10).until(EC.element_to_be_clickable, ((By.XPATH,"//button[#type='button' and contains(.,'Sign In')]")))
botton_to_click.click()
browser.find_element_by_id('username').send_keys('##$%*&^%##$')
browser.find_element_by_id('password').send_keys('##*$%^!#')
browser.find_element_by_id('basic-login').click()
browser.find_element_by_id('masthead-container').click()
browser.find_element_by_id('searchInput').send_keys('cybersecurity')
browser.find_element_by_name('ADVANCED SEARCH').click()
browser.find_element_by_id('dp1560924131783').send_keys('2018/04/01')
browser.find_element_by_id('dp1560924131784').send_keys('2019/03/31')
browser.find_element_by_id('wsjblogs').click()
browser.find_element_by_id('wsjvideo').click()
browser.find_element_by_id('interactivemedia').click()
browser.find_element_by_id('sitesearch').click()
browser.close()
Thanks.
Remove the comma after element_to_be_clickable as given below, It may resolve your issue.
botton_to_click = WebDriverWait(browser, 10).until(EC.element_to_be_clickable, ((By.XPATH,"//button[#type='button' and contains(.,'Sign In')]")))
to
botton_to_click = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[#type='button' and contains(.,'Sign In')]")))