Chromedriver selenium python - python

i am working on a python project using selenium.
I get the following error and would like some help please
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot determine loading status
from unknown error: unexpected command response
(Session info: chrome=103.0.5060.114)
My chrome is version 103, and so is my chrome driver.
Here is the code
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.webdriver.chrome.service import Service
player = input("Which player: ").lower()
PATH = "/Users/makfadel/Desktop/Desktop-items/chromedriver 2"
#driver = webdriver.Chrome(PATH)
s = Service(PATH)
browser = webdriver.Chrome(service=s)
browser.get("https://www.basketball-reference.com/leagues/NBA_2022_per_game.html")
search = browser.find_element(By.XPATH, "//*[#id='header']/div[3]/form/div/div/input[2]")
search.send_keys(player)
search.send_keys(Keys.RETURN)
try:
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='players']/div[1]/div[1]/strong/a")))
element.click()
name = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='meta']/div[2]/h1/span")))
print()
print(name.text)
print()
szn = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[1]/div/p[1]/strong"))
)
games = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[2]/div[1]/p[1]"))
)
ppgame = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[2]/div[2]/p[1]"))
)
rebounds = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[2]/div[3]/p[1]"))
)
assists = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[2]/div[4]/p[1]"))
)
fg = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[3]/div[1]/p[1]"))
)
fg3 = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[3]/div[2]/p[1]"))
)
ft = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[3]/div[3]/p[1]"))
)
print(f"Season: {szn}")
print(f"Games played: {games.text}")
print(f"Points per game: {ppgame.text}")
print(f"Rebounds per game: {rebounds.text}")
print(f"Assists per game: {assists.text}")
print(f"Field goal percentage: {fg.text}%")
print(f"Field goal from 3 percentage: {fg3.text}%")
print(f"Free throw percentage: {ft.text}%")
except Exception:
print(f"No player named {player}")
finally:
browser.quit()

There has been an issue with chrome driver 103 version
Please find below the bug ids for the same,
https://bugs.chromium.org/p/chromedriver/issues/detail?id=4121&q=label%3AMerge-Request-103
Check it on GitHub Selenium Issue - https://github.com/SeleniumHQ/selenium/issues/10799
Solution
For now, until this issue is fixed try to "Downgrade Chrome Browser To v102" and "Download Selenium Chrome Driver 102" and try to run your script, as this issue is happening in 103 version.
Upgrade the chromedriver to v104
sample code snippet -
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
// 1
option = Options()
option.binary_location='/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta'
// 2
driver = webdriver.Chrome(service=Service(ChromeDriverManager(version='104.0.5112.20').install()), options=option)
code reference - https://www.globalnerdy.com/2022/06/30/fix-the-chromedriver-103-bug-with-chromedriver-104/

Related

How to print messages in console in Python Selenium

I'm using below code to test login to a website.The code opens a webpage and wait for an element to load and then logs in using the provided credentials. I have put some messages in the code for me to identify whether the code is running. However, it is not working.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
s = Service("/usr/local/bin/chromedriver")
url = "https://atf.domain.com/"
driver = webdriver.Chrome(options=options, service=s)
driver.get(url)
print(driver.title)
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
print("Page loaded!")
)
except TimeoutException:
print("Page not loaded!")
driver.find_element(By.ID, "username").send_keys("email#gmail.com")
driver.find_element(By.ID, "password").send_keys("password")
driver.find_element(By.ID, "signinButton").click()
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "compose-delivery-link"))
print("Compose delivery page loaded!")
)
except TimeoutException:
print("Compose delivery page not loaded!")
driver.find_element(By.ID, "compose-delivery-link").click()
driver.close()
I'm getting below error,
File "sft_login_test.py", line 24
print('Page loaded!')
^
SyntaxError: invalid syntax
I have no tried idea how to fix this. Any help is greatly appreciated.
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
)
print("Page loaded!")
I guess the print call should be outside the .until arguments.
You accidentally wrote a call to print() inside the parentheses of .until()
Both at line 23
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
print("Page loaded!")
)
and at line 37
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "compose-delivery-link"))
print("Compose delivery page loaded!")
)
Additionally, you're gonna want to import the TimeoutException from selenium.common.exceptions, because that's likely the next error you will encounter.
And to give some side Information, WebDriverWait doesn't actually wait 10 seconds before looking for the element. It rather checks for the condition (here the presence of an element with ID "name" or "password" respectively) in a set interval until it either finds the element, in which case the element is returned, or the time limit (here 10 seconds) is reached, in which case it throws a TimeoutException.

How to click recaptchaV2's Solve the challenge button using Selenium and Python

I'm trying to interact with the recaptchaV2 Solve the challenge button on image verification popup using Selenium and Python.But meet some problem.By the way,I use buster chrome extension to bypass the recaptcha.Hope can help me.Thank you~
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_extension('~/Library/Application Support/Google/Chrome/Default/Extensions/mpbjkejclgfgadiemmefgebjfooflfhl/1.1.0_0.crx')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.google.com/recaptcha/api2/demo")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#solver-button"))).click()
problem like
chrome_options = webdriver.ChromeOptions()
Is outdated use Options.
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
Also the audio button is
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#recaptcha-audio-button"))).click()
Not
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#solver-button"))).click()
It also detects automation so use
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
Evil company was change class name in [challenge]
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge expires in two minutes']")))
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.get('https://www.google.com/recaptcha/api2/demo')
element_present = driver.find_element(By.XPATH, '//*[#title="reCAPTCHA"]')
driver.switch_to.frame(element_present)
element_present = EC.presence_of_element_located((By.XPATH, '//*[#id="recaptcha-anchor"]/div[1]'))
WebDriverWait(driver, 15, poll_frequency=POLL_FREQUENCY).until(element_present).click()
driver.switch_to.default_content()
time.sleep(5)
element_present = driver.find_element(By.XPATH, "/html/body/div[2]/div[4]/iframe")
driver.switch_to.frame(element_present)
try:
buttonHolderElement = driver.find_element(By.XPATH, '//*[#id="rc-imageselect"]/div[3]/div[2]/div[1]/div[1]/div[4]')
actions = ActionChains(driver)
actions.move_to_element(buttonHolderElement)
actions.click(buttonHolderElement)
actions.perform()
except:
pass

click function not rendering the page

I am trying to retrieve PNR details but the click function runs into a timeout exception. What could be the possible issue that causes this timeout?
from selenium import webdriver
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(executable_path=r'D:/Chrome driver/chromedriver.exe')
driver.get("link")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='PNRId']"))).send_keys("QPDYUX")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='GstRetrievePageInteraction']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "gst-invoice-list.list-inline")))
elements = driver.find_elements(By.CLASS_NAME, "gst-invoice-list.list-inline")
It gives following error TimeoutException: timeout: Timed out receiving message from renderer: 300.000 (Session info: chrome=87.0.4280.141)
How can I move forward with this?
Try adding the following to stop the website from knowing it's a bot.
from selenium.webdriver.chrome.options import Options
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(executable_path=r'D:/Chrome driver/chromedriver.exe',options=options)
Now for the following:
wait = WebDriverWait(driver, 10)
driver.get("link")
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='PNRId']"))).send_keys("QPDYUX")
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='GstRetrievePageInteraction']"))).click()
elements = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".gst-invoice-list.list-inline")))
for elem in elements:
print(elem.text)
Outputs:
GST Invoice No. View/Print
MH1202106AB57221 View Invoice Print Invoice
MH2202106AB78553 View Invoice Print Invoice

Can't find a way to click on an href link with selenium (Python 3.7)

Pic of inspect element
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('https://website.com/')
driver.maximize_window()
search = driver.find_element_by_id('UserName')
search.send_keys('UserName')
search = driver.find_element_by_id('Password')
search.send_keys('Password')
search.send_keys(Keys.RETURN)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.LINK_TEXT, "Admin"))
)
element.click
link = driver.find_element_by_link_text('Admin')
link.click()
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.LINK_TEXT, "Reports"))
)
element.click
link = driver.find_element_by_link_text('Reports')
link.click()
except:
driver.quit()
driver.implicitly_wait(5)
sales_link = driver.find_element_by_link_text('Sales').click()
Below is the info from the website, I want to click on Sales but can't seem to do so any help would be appreciated
a _ngcontent-hyf-c12="" routerlink="./SalesReport" routerlinkactive="active" href="/Reports/SalesReport"Sales /a
Pic of error
This what appears if I try to click on it with XPATH
Error Pic
In your html pic, I see a whitespace after Sales.
Look carefully: href="/Reports/SalesReport">Sales </a.
So find_element_by_link_text('Sales') will not work.
You can change it to find_element_by_link_text('Sales ').
However, this will be better:
driver.find_elements_by_xpath("//a[contains(text(), 'Sales')]")

Selenium Login failing - wrong ids?

This is just a quick sanity check I'm hoping someone can put some eyes on.
Basically, I'm trying to log into this site (ArcGIS Online) with selenium: https://www.arcgis.com/home/signin.html?useLandingPage=true".
This is what the elements look like:
My code for the login looks like this:
user = driver.find_element_by_name("user_username")
password = driver.find_element_by_name("user_password")
user.clear()
user.send_keys(username)
password.clear()
password.send_keys(password)
driver.find_element_by_id("signIn").click()
In this case, I'm using the ArcGIS login option...not sure if I need to be activating that first? I'm assuming there's a step I'm missing here to get this to work.
The error I get currently is:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="user_username"]"}
Really appreciate any thoughts people have out there!
EDIT:
Updated code to this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.headless = True
browser = webdriver.Chrome(options=options)
url = "https://www.arcgis.com/home/signin.html?useLandingPage=true"
browser.get(url)
try:
user = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "user_username"))
)
password = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "user_password"))
)
signin = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "signIn"))
)
user.clear()
user.send_keys(username)
password.clear()
password.send_keys(password)
signin.click()
finally:
browser.quit()
And now get this error:
TypeError: object of type 'WebElement' has no len()
Probably doing something wrong, will look deeper into the proper usage of wait functionality.
FINAL EDIT:
For anyone chancing upon this page, the solution was to wait for elements to load, make sure I was searching by IDs, and...make sure I had no typos! Below is a working scenario:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
username = "Hari"
password = "Seldon"
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)
url = "https://www.arcgis.com/home/signin.html?useLandingPage=true"
driver.get(url)
try:
user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "user_username")))
passwd = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "user_password")))
user.clear()
user.send_keys(username)
passwd.clear()
passwd.send_keys(password)
driver.find_element_by_id("signIn").click()
finally:
driver.quit()
You should wait for the elements to load on the page first:
user = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "user_username")))
password = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "user_password")))
user.clear()
user.send_keys("aaa")
password.clear()
password.send_keys("bbb")
driver.find_element_by_id("signIn").click()
You need these imports for the wait functionality:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
You are using find_element_by_name but using the value of id in the code. I changed it to find_element_by_id and it works. Code below:
user = browser.find_element_by_id("user_username")
password = browser.find_element_by_id("user_password")
user.clear()
user.send_keys(username)
password.clear()
password.send_keys(password)
browser.find_element_by_id("signIn").click()

Categories