'python selenium for loop

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



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 morehardthansmartwork