如何在 Python 中为 Selenium 动态创建基于系统变量的路径

How to Make a Path Dynamically Based off System Variable in Python For Selenium

所以我试图做一个 if 语句来弄清楚如何动态设置 selenium 使用的变量的路径。最主要的是我希望语句查看是否安装了驱动程序,然后如果未安装驱动程序则根据 platform 模块中的 platform.system() 函数中断。我有以下内容,但我遇到了无效的语法问题。我有工作路径并安装在 windows 和 Linux 系统上,所以我知道它们工作。

import selenium
import shutil
import xlsxwriter
import os
import unittest
import requests
import subprocess
import getpass
import platform
import logging
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from datetime import date

# Definitions
# find_elements_by_name
# find_elements_by_xpath
# find_elements_by_link_text
# find_elements_by_partial_link_text
# find_elements_by_tag_name
# find_elements_by_class_name
# find_elements_by_css_selector

# System Variables
date = today.strftime("%m/%d/%Y")
system = platform.system()
today = date.today()
username = getpass.getuser()

# URL Variables 
login_url = 'https://www.accuplacer.org/'
redirect_url = 'https://www.accuplacer.org/api/home.html#/'
reports_scheduler_url = 'https://www.accuplacer.org/api/home.html#/reportScheduler'
custom_reports_url = 'https://www.accuplacer.org/api/home.html#/customReports'

# WebDriver Path for System
if system = ('Windows'):
      browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
elif system = ('Linux'):
      broswer = webdriver.Chrome("~/Drivers/Google/Chrome/chromedriver_linux64")
elif system = ('Darwin'):
      browser = webdriver("~/Drivers/Google/Chrome/chromedriver_mac64")
else:
      print("Are you sure you have the Selenium Webdriver for Chrome installed in the correct path?")
      continue

# Parent URL
browser.get("https://www.accuplacer.org")

当我尝试打开 Linux 或 Windows 中的站点时,出现以下语法错误:

  File "secret_collegeboard_tsi_export.py", line 56
    if system = ('Windows'):
              ^
SyntaxError: invalid syntax

改用这个:

if system == ('Windows'):
    browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
elif system == ('Linux'):
    broswer = webdriver.Chrome("~/Drivers/Google/Chrome/chromedriver_linux64")
elif system == ('Darwin'):
    browser = webdriver("~/Drivers/Google/Chrome/chromedriver_mac64")

= 是赋值运算符 == 是比较运算符

=用于给avariable赋值。对于 equality 检查 string 使用

if system is 'Windows':
      browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")

if system == 'Windows':
      browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")

其实我很快就想通了。我使用了以下内容,类似于@Muzzamil 和@testfile 提到的内容:

# WebDriver Path for System
if platform.system() == ('Windows'):
    browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
elif platform.system() == ('Linux'):
    browser = webdriver.Chrome(executable_path='/home/rbarrett/Drivers/Google/Chrome/chromedriver_linux64/chromedriver')
elif platform.system() == ('Darwin'):
    browser = webdriver(executable_path='~/Drivers/Google/Chrome/chromedriver_mac64/chromedriver')
else:
    print("Are you sure you have the Selenium Webdriver installed in the correct path?")