python selenium for loop - python

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
PATH ="/Users/kaikeichan/Desktop/python_webpage/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://tsj.tw/")
blow = driver.find_element(By.ID,'click')
blow_count = driver.find_element(By.XPATH,'//*[#id="app"]/div[2]/div[4]/div[2]/h4[2]')
items = []
items.append(driver.find_element(By.XPATH,'//*[#id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[4]/td[5]/button[1]'))
items.append(driver.find_element(By.XPATH,'//*[#id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[3]/td[5]/button[1]'))
items.append(driver.find_element(By.XPATH,'//*[#id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[2]/td[5]/button[1]'))
prices = []
prices.append(driver.find_element(By.XPATH,'//*[#id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[4]/td[4]'))
prices.append(driver.find_element(By.XPATH,'//*[#id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[3]/td[4]'))
prices.append(driver.find_element(By.XPATH,'//*[#id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[2]/td[4]'))
actions = ActionChains(driver)
actions.click(blow)
for i in range(100):
actions.perform()
count = int(blow_count.text.replace("您目前擁有", "").replace("技術點", ""))
for j in range(3):
price = int(prices[j].text.replace("技術點", ""))
if count >= price:
upgrade_actions = ActionChains(driver)
upgrade_actions.move_to_element(items[j])
upgrade_actions.click()
upgrade_actions.perform()
break
raceback (most recent call last):
File "/Users/kaikeichan/Desktop/python_webpage/actionchain.py", line 7, in
driver = webdriver.Chrome(PATH)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in init
super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog",
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 90, in init
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 110, in assert_process_still_running
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service /Users/kaikeichan/Desktop/python_webpage/chromedriver unexpectedly exited. Status code was: -9

From what I see you have an error in line 7 as it's pointed out in the traceback.
In the line of code you define your path you're trying to use an absolute path.
PATH ="/Users/kaikeichan/Desktop/python_webpage/chromedriver"
The problem in here may be that you are not writing the right path to the chromedriver file. Your path should have a C: before user (that is if you have your project stored in your computer and not an external hard drive or something similar) like this:
PATH ="C:/Users/kaikeichan/Desktop/python_webpage/chromedriver"
Another thing you could try is adding a ".exe" in the end of chromdriver as it is a .exe file and you should specify that.
PATH ="C:/Users/kaikeichan/Desktop/python_webpage/chromedriver.exe"
I really hope this helps and I would suggest you try to use a relative path because it could be simpler to you. If your ".py" python file is in the same folder your chromdriver file is allocated then you could just define the path as "chromedriver.exe" and that would be an effective use of a relative path.

Related

ValueError: Could not get version for Chrome with this command: reg query

I am using selenium with python and have downloaded the chromedriver for my windows computer from this site. After downloading the zip file, I unpacked the zip file to my downloads folder. Then I add the path to the the Environment Variable "Path".
I want to get information from the site, but when I run code, I get this error and comletely don't understand what is it about.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())
url = 'any_url'
browser.get(url)
if browser.find_element_by_id("yearlist_1").get_attribute("type") == "checkbox":
print("Element is a checkbox")
else:
print("Element is not a checkbox")
The error:
*'reg' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
File "C:/Users/miair/Python/script_gks/code_for_ticks.py", line 4, in <module>
browser = webdriver.Chrome(ChromeDriverManager().install())
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\chrome.py", line 28, in install
driver_path = self.download_driver(self.driver)
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\manager.py", line 36, in download_driver
driver_version, is_latest = self.__get_version_to_download(driver)
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\manager.py", line 27, in __get_version_to_download
return self.__get_latest_driver_version(driver), True
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\manager.py", line 21, in __get_latest_driver_version
return driver.get_latest_release_version()
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\driver.py", line 58, in get_latest_release_version
self._latest_release_url + '_' + chrome_version(self.chrome_type))
File "C:\Program Files\Python37\lib\site-packages\webdriver_manager\utils.py", line 114, in chrome_version
.format(cmd)
ValueError: Could not get version for Chrome with this command: reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version*
Sorry, I am very new to Python, but if somebody could help, I'll be very pleased and thankful.
I solved problem. I don't understand why I've hadn't think of this solution before - all you need to do is to write the path of your chromedriver (only if you have a correct version)
browser = webdriver.Chrome('path')
important to know for people searching for the same error and ending here. chrome needs to have started at least once to generate the reg key in windows.
I suspect that you are using a selenium web-driver that is not matching your chrome driver version.
you can update your chrome using the following link:
chrome://settings/help

selenium python problem with loading page driver.get('http://page.com/') open empty window

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
browser = webdriver.Firefox(executable_path=r'/usr/bin/firefox')
browser.get("http://google.com/")
Ubuntu 16 LTS user here.How to fix this empty window issue after I enter url and is no loaded page there.
Output:
> > Traceback (most recent call last): File "/home/pc/PycharmProjects/calculator/test.py", line 781, in <module>
> browser = webdriver.Firefox(executable_path=r'/usr/bin/firefox') File
> "/home/pc/PycharmProjects/calculator/venv/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py",
> line 164, in __init__
> self.service.start() File "/home/pc/PycharmProjects/calculator/venv/lib/python3.5/site-packages/selenium/webdriver/common/service.py",
> line 98, in start
> self.assert_process_still_running() File "/home/pc/PycharmProjects/calculator/venv/lib/python3.5/site-packages/selenium/webdriver/common/service.py",
> line 111, in assert_process_still_running
> % (self.path, return_code) selenium.common.exceptions.WebDriverException: Message: Service
> /usr/bin/firefox unexpectedly exited. Status code was: 0
You need to provide geckodriver path not firefox. Check your firefox browser version and you can download compatible geckodriver from Here
browser = webdriver.Firefox(executable_path="path of geckodriver")
Sample code
browser = webdriver.Firefox(executable_path="/Users/username/Location/geckodriver")
browser.get("https://google.com")
You can add the path to your webdriver in the PATH system variable
export PATH=$PATH:/path/to/driver/firefox-driver
Add it to /home//.profile file to make it permanent.
Then you dont need executable_path=''

Selenium WebDriverException 'chromedriver.exe' needs to be in PATH

I'm trying to use selenium for a python web scraper but when I try to run the program I get the following error:
/usr/local/bin/python3 /Users/xxx/Documents/Python/hello.py
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/xxx/Documents/Python/chromedriver.exe'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/xxx/Documents/Python/hello.py", line 9, in <module>
wd = webdriver.Chrome(executable_path=DRIVER_PATH)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Here is the python code:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
from selenium import webdriver
DRIVER_PATH = '/Users/xxx/Documents/Python/chromedriver.exe'
wd = webdriver.Chrome(executable_path=DRIVER_PATH)
I think the problem is that I'm not specifying the file path in the variable DRIVER_PATH properly but I'm not sure
I am using a Mac
You need to update DRIVER_PATH to include your root directory, which is usually C:\:
DRIVER_PATH = 'C:/Users/xxx/Documents/Python/chromedriver.exe'
Alternatively, you can follow this tutorial to add the path to containing folder of chromedriver.exe (usually chromedriver_win32 folder) to your Path environment variable:
https://docs.telerik.com/teststudio/features/test-runners/add-path-environment-variables
I would try this out (Just adding the 'r'):
wd = webdriver.Chrome(executable_path=r'/Users/xxx/Documents/Python/chromedriver.exe')
if you think it's the filepath then have a go with checking:
import os.path
os.path.exists(DRIVER_PATH)
Also, Beautifulsoup is used will with urllib2
https://www.pythonforbeginners.com/beautifulsoup/beautifulsoup-4-python
import urllib2
url = "https://www.URL.com"
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content)
You have a mistake in the name of the file.
"chomedriver.exe" is for windows.
If you use macOS and chromedriver for Mac, then the file name should be "chomedriver" without ".exe".
I had the same problem, but this solved it.

Python Selenium Error Chromedriver Permission [duplicate]

This question already has answers here:
'Webdrivers' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
(22 answers)
Closed 4 years ago.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
Game_Pin = input('Enter your PIN: ')
NickNAME = input('Enter your nickname: ')
def Enter_Press():
browser.find_element_by_name("Enter").click()
def Kahoot_Spammer(Game_Pin, NickNAME):
chromedriver = webdriver.Chrome(r'C:\WebDriver\bin\chromedriver_win32')
browser = webdriver.Chrome(chromedriver)
browser.get('https://kahoot.it/')
game_pin = browser.find_element_by_id("Game PIN")
Name = browser.find_element_by_id("Nickname")
game_pin.send_keys(Game_Pin)
Enter_Press()
Name.send_keys(NickNAME)
Enter_Press()
Kahoot_Spammer(Game_Pin, NickNAME)
Wondering what I have done wrong in the code above. The file I have put into the path and have ran the
icacls "pathtochromedriver" /grant Users:F
command through the command prompt but it still didn't fix the problem. I have been stuck on this problem for quite some while now.
Traceback (most recent call last):
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\KahootSpammer.py", line 28, in <module>
Kahoot_Spammer(Game_Pin, NickNAME)
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\KahootSpammer.py", line 13, in Kahoot_Spammer
chromedriver = webdriver.Chrome(r'C:\WebDriver\bin\chromedriver_win32')
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
self.service.start()
File "C:\Users\ovvip\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 88, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver_win32' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Any ideas on how to fix this? Every time I ask this question I usually end up with a answer that will fix one thing but then another problem arises.
Try using webdriver.Chrome(executable_path=r"C:\WebDriver\bin\chromedriver_win32")
If that doesn't solves the problem than I think there might be an issue with the version of chrome driver you are using download chromedriver from this path
Chromedriver Download Link
and paste it in the directory
and test it for this code it should run smooth without any error
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path=r"C:\WebDriver\bin\chromedriver.exe")
for j in range(2,8):
for i in range(2,8):
driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
str1 = '//*[#id="offers_table"]/tbody/tr['
str2 = ']/td/table/tbody/tr[1]/td[2]/h3/a/span'
str3 = str1 + str(i) + str2
a = driver.find_element_by_xpath(str3).text
print a
Comment below if you face any issue
for one thing:
chromedriver = webdriver.Chrome(r'C:\WebDriver\bin\chromedriver_win32')
browser = webdriver.Chrome(chromedriver)
^^ Here you create a webdriver.Chrome instance named chromedriver. Then on the next line, you attempt to create another webdriver.Chrome instance... but this time passing the existing instance to it? Not sure why you are attempting to create multiple instances of it at all, but that code is definitely not going to work. Just create a single instance of webdriver.Chrome and use it:
browser = webdriver.Chrome(r'C:\WebDriver\bin\chromedriver_win32')

Issue with selenium-python on Firefox

I am new to Selenium and am following the website http://selenium-python.readthedocs.org/
I tried the following bit of code which is there on the website
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
When I run this program, a Firefox instance opens but never loads the page. check snapshot
This is what I have received in the output console
C:\Users\Gauss\Desktop>python test.py
Traceback (most recent call last):
File "test.py", line 4, in <module>
driver = webdriver.Firefox()
File "D:\Python\Python35\lib\site-packages\selenium-2.50.0-py3.5.egg\selenium\webdriver\firefox\webdriver.py", line 78, in __init__
self.binary, timeout)
File "D:\Python\Python35\lib\site-packages\selenium-2.50.0-py3.5.egg\selenium\webdriver\firefox\extension_connection.py", line 49, in __init__
self.binary.launch_browser(self.profile)
File "D:\Python\Python35\lib\site-packages\selenium-2.50.0-py3.5.egg\selenium\webdriver\firefox\firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable()
File "D:\Python\Python35\lib\site-packages\selenium-2.50.0-py3.5.egg\selenium\webdriver\firefox\firefox_binary.py", line 106, in _wait_until_connectable
% (self.profile.path))
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: C:\Users\Gauss\AppData\Local\Temp\tmpkpjhnhpb If you specified a log_file in the FirefoxBinary constructor, check it for details.
Could someone please suggest a solution to this issue?
Note: This issue is on my office pc and the same code works fine on my home pc. So some issue with Firefox maybe.
Based on your comments, it looks like the version of Firefox you have is not yet supported by python driver. Currently they support up to v. 38, you have 44.

Categories