Python, Selenium "'bool' object is not callable" Error - python

I'm trying to prepare a bot for a platform with using python and selenium. My codes are below. When I runned I saw a error like that:
Traceback (most recent call last):
File "C:\Users\doguk\Desktop\Python\eksi_01.py", line 76, in <module>
WebDriverWait(driver,30).until(recaptchaSolved.is_displayed())
File "C:\Users\doguk\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
TypeError: 'bool' object is not callable
## necessary import codes
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time
import xlrd
import random
## random waiting time
random_Get_Time = random.randint(1,5)
## importing data file
data_File_Path = "C:/Users/doguk/Desktop/Python/DataFile.xls"
## targeting the data
dataFile = xlrd.open_workbook(data_File_Path)
accountInfo = dataFile.sheet_by_name("account_Ids_And_Passwords")
usernameCount = accountInfo.nrows
passwordCount = accountInfo.ncols
for curr_row in range(1, usernameCount):
username = accountInfo.cell_value(1, 0)
password = accountInfo.cell_value(1, 1)
## setting up recaptcha solver plugin to browser
recaptcha_Solver_Plugin_Path = "C:/Users/doguk/Desktop/Python/plugin.zip"
## adding recaptcha solver plugin to browser
recaptchaSolverAddOptions = webdriver.ChromeOptions()
recaptchaSolverAddOptions.add_extension(recaptcha_Solver_Plugin_Path)
## browser driver code
chrome_Driver_Path = "C:/Users/doguk/Desktop/Python/chromedriver"
driver = webdriver.Chrome(chrome_Driver_Path, options=recaptchaSolverAddOptions)
## enter the browser
driver.get("**URL**")
time.sleep(random_Get_Time)
## fullscreen code
driver.maximize_window()
## filling account infos
login_Form_Username = driver.find_element_by_id('username')
login_Form_Username.send_keys(username)
time.sleep(random_Get_Time)
login_Form_Password = driver.find_element_by_id('password')
login_Form_Password.send_keys(password)
## waiting for captcha confirmation
recaptchaDisplayed = driver.find_element_by_class_name('g-recaptcha')
if(recaptchaDisplayed.is_displayed()):
recaptchaSolved = driver.find_element_by_partial_link_text('Solved')
WebDriverWait(driver,30).until(recaptchaSolved.is_displayed())
else:
time.sleep(random_Get_Time)
## log in to platform
login_Button = driver.find_element_by_class_name('btn')
login_Button.click()
time.sleep(random_Get_Time)
## logout from driver
driver.quit()
How can I fix that?
What I'm trying to do is there is a Google Captcha confirmation on login page. If there isn't don't wait much. If there is, wait until recaptcha solver plugin fix it.
If I remove "## waiting for captcha confirmation" section and add time.sleep(110) it will solved. But I need to make that similar with human. I'm waiting for your helps.
Instead of
WebDriverWait(driver,30).until(recaptchaSolved.is_displayed())
I tried that
WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH, "//*[contains(text(),'Solved')]")))
But it gave an error again. Like that:
Traceback (most recent call last):
File "C:\Users\doguk\Desktop\Python\eksi_01.py", line 75, in <module>
recaptchaSolved = driver.find_element_by_partial_link_text('Solved')
File "C:\Users\doguk\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 462, in find_element_by_partial_link_text
return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)
File "C:\Users\doguk\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\doguk\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\doguk\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"Solved"}
(Session info: chrome=92.0.4515.159)

Related

unable to access HTML Select Option using selenium getting error. ElementNotInteractableException

I am trying to scrape data from this File-tuning dynamic website which is loading data through javascript (ajax) requests.
what is want to do is that It selects cars from type and then selects make, model, engine iteratively, and then I want to scrape data for each make, model, and engine.
Here is the code I write to select cars from type
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
PATH = "C:\SeleniumDrivers\geckodriver.exe"
driver = webdriver.Firefox(executable_path=PATH)
driver.get("https://file-tuning.com/chiptuning")
type_element_select = driver.find_element_by_id("type")
action = ActionChains(driver)
action.move_to_element(type_element_select)
action.click(type_element_select)
action.perform()
action.move_to_element(Select(type_element_select).select_by_value("cars"))
action.click(Select(type_element_select).select_by_value("cars"))
action.perform()
The error I get:
Traceback (most recent call last):
File "D:\Python\selenium\test.py", line 27, in <module>
action.move_to_element(Select(type_element_select).select_by_value("cars"))
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.py", line 82, in select_by_value
self._setSelected(opt)
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.py", line 212, in _setSelected
option.click()
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view
I also try this
types_element = driver.find_element_by_id("type")
types_object = Select(types_element)
types_object.select_by_visible_text("Cars")
but it also gives me the same exception.
how I can scrape through this site?
already have seen and tried other StackOverflow related question but didn't work out for me.
You need to use browser in full screen mode :
driver.maximize_window()
so that all web elements would be visible to Selenium.
Sample code :
PATH = "C:\SeleniumDrivers\geckodriver.exe"
driver = webdriver.Firefox(executable_path=PATH)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://file-tuning.com/chiptuning")
wait = WebDriverWait(driver, 10)
type_element_select = driver.find_element_by_id("type")
action = ActionChains(driver)
action.move_to_element(type_element_select)
action.click(type_element_select)
action.perform()
action.move_to_element(Select(type_element_select).select_by_value("cars"))
action.click(Select(type_element_select).select_by_value("cars"))
action.perform()

Error in Selenium webdriver on clicking on button

I've error in Python Selenium. I'm trying to download all songs with Selenium, but there is some error. Here is code:
from selenium import webdriver
import time
driver = webdriver.Chrome('/home/tigran/Documents/chromedriver/chromedriver')
url = 'https://sefon.pro/genres/shanson/top/'
driver.get(url)
songs = driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div[1]/div[3]/div/div[3]/div[2]/a')
for song in songs:
song.click()
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[3]/div[1]/a[2]').click()
time.sleep(8)
driver.get(url)
time.sleep(5)
And here is error:
Traceback (most recent call last):
File "test.py", line 13, in <module>
song.click()
File "/home/tigran/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/home/tigran/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/home/tigran/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/tigran/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=90.0.4430.72)
Any ideas why error comes?
Before your loop try to add try:
Sometimes the document is not attached, so it gives an error
Here's link selenium while loop not working. You need to locate elements in loop.
Final code:
from selenium import webdriver
import time
driver = webdriver.Chrome('/home/tigran/Documents/chromedriver/chromedriver')
url = 'https://sefon.pro/genres/shanson/top/'
driver.get(url)
size = len(driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div[1]/div[3]/div/div[3]/div[2]/a'))
for i in range(0, size):
songs = driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div[1]/div[3]/div/div[3]/div[2]/a')
songs[i].click()
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[3]/div[1]/a[2]').click()
time.sleep(8)
driver.execute_script("window.history.go(-1)")
time.sleep(5)

Element can't be accessed when using selenium in python

I'm trying to log in to linked in and access the "My network" element. I've tried using id, class, xpath, and nothing works. I also tried waiting for 10 secs for the page to load so that python can access the site but that gives an error too. Here's my code:
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
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
username=input("Enter linkedin mail id: ")
password=input("Enter password: ")
driver=webdriver.Chrome(PATH)
driver.get("https://in.linkedin.com/")
search = driver.find_element_by_class_name("nav__button-secondary")
search.click()
# logging in to linkedin
search=driver.find_element_by_id("username")
search.send_keys(username)
search.send_keys(Keys.RETURN)
search=driver.find_element_by_id("password")
search.send_keys(password)
search = driver.find_element_by_class_name("login__form_action_container ")
search.click()
search=driver.find_element_by_xpath("//div[#ID='ember748']") # DOESNT WORK
search.click()
I'm able to log in to LinkedIn using my username and password but after that, trying to access my network throws an error. What mistake am I making here?
This is what pops up in cmd:
DevTools listening on ws://127.0.0.1:61153/devtools/browser/4a54a771-4361-4304-a2df-7a366a4f31cc
[27720:25060:0412/123447.314:ERROR:device_event_log_impl.cc(214)] [12:34:47.315] USB: usb_device_handle_win.cc:1056 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
This is the error I'm getting:
Traceback (most recent call last):
File "C:\Users\vedha\OneDrive\Documents\GitHub\Internship\linkedinT1.py", line 32, in
search=driver.find_element_by_xpath("//div[#ID='ember748']") # DOESNT WORK
File "C:\Users\vedha\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\vedha\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\vedha\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\vedha\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: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#ID='ember748']"}
(Session info: chrome=89.0.4389.114)

Selenium WebDriver Exception: Message: unknown error: Failed to create Chrome process

I have tried all fixes but for some reason, exceptions are coming in my code. Please help me out. The code block is trying to automate a mass WhatsApp messaging bot.
The code was adapted from a publicly available GitHub repository.
The chrome version is updated to the newest release. I am using python 3.9 environment with the latest pip installer, updated selenium library, and the most recent chromedriver extension.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from time import sleep
from urllib.parse import quote
options = Options()
#options.add_argument("user-data-dir=/tmp/tarun")
#options.add_argument("user-data-dir=C:\\Users\\anirudh_bagri\\AppData\\Local\\Google\\Chrome\\User Data")
f = open("message.txt", "r")
message = f.read()
f.close()
print('This is your message:')
print(message)
message = quote(message)
numbers = []
f = open("numbers.txt", "r")
for line in f.read().splitlines():
if line != "":
numbers.append(line)
f.close()
print('\nWe found ' + str(len(numbers)) + ' numbers in the file')
delay = 30
print('Once your browser opens up, make sure you sign in to web whatsapp and then press enter')
driver = webdriver.Chrome(executable_path=r'C:\Users\LEGION\Desktop\whatsapp-bulk-messenger-master\chromedriver.exe', options=options)
driver.get('https://web.whatsapp.com')
input()
for number in numbers:
if number == "":
continue
print('Sending message to: ' + number)
try:
url = 'https://web.whatsapp.com/send?phone=' + number + '&text=' + message
driver.get(url)
click_btn = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.CLASS_NAME , '_3M-N-')))
click_btn.click()
sleep(1)
print('Message sent to: ' + number)
except Exception:
print('Failed to send message to ' + number)
This is the exception that is coming up.
= RESTART: C:\Users\LEGION\Desktop\whatsapp-bulk-messenger-master\automator.py =
This is your message:
Hello World,
This is my text to you from automated messaging system.
Thank You
We found 1 numbers in the file
Once your browser opens up, make sure you sign in to web whatsapp and then press enter
Traceback (most recent call last):
File "C:\Users\LEGION\Desktop\whatsapp-bulk-messenger-master\automator.py", line 31, in <module>
driver = webdriver.Chrome(executable_path=r'C:\Users\LEGION\Desktop\whatsapp-bulk-messenger-master\chromedriver.exe', options=options)
File "C:\Users\LEGION\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "C:\Users\LEGION\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\LEGION\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\LEGION\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\LEGION\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create Chrome process.
>>>
This most probably happens when you run Chrome as an administrator. Try running it "not as an admin" and check once.
Just got to Chrome's properties->compatibility and change that

Unable to find element and input value using selenium

My objective is to take a value (in my case a shipment tracking #) and input it into a tracking field on a website using selenium. I am unable to input the value and get the following error message:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "/Users/GUVA/Downloads/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.17track.net/en")
#data = df.values[1] # grabbing one tracking number from an excel file
# click "agree" to close window
python_button = driver.find_elements_by_xpath('//*[#id="modal-gdpr"]/div/div/div[3]/button')[0]
python_button.click()
# enter tracking number into text box
que=driver.find_element_by_id('//input[#id="jsTrackBox"]')
que.send_keys("data")
Traceback (most recent call last):
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/Track Shipment.py", line 26, in <module>
que=driver.find_element_by_id('//input[#id="jsTrackBox"]')
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=83.0.4103.61)
Any suggestions please?
//input[#id="jsTrackBox"] is an xpath. So you need to fetch the element by using find_element_by_xpath method(where you are currently using find_element_by_id method).
Your code should be like:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "/Users/GUVA/Downloads/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.17track.net/en")
#data = df.values[1] # grabbing one tracking number from an excel file
# click "agree" to close window
python_button = driver.find_elements_by_xpath('//*[#id="modal-gdpr"]/div/div/div[3]/button')[0]
python_button.click()
# enter tracking number into text box
que=driver.find_element_by_xpath("//div[#id='jsTrackBox']//div[#class='CodeMirror-scroll']")
que.click()
que.send_keys("data")

Categories