如何在Python For Selenium中动态生成基于非系统变量的路径

如何在Python For Selenium中动态生成基于非系统变量的路径,python,selenium,selenium-chromedriver,platform,browser-automation,Python,Selenium,Selenium Chromedriver,Platform,Browser Automation,因此,我尝试使用if语句来了解如何动态设置selenium使用的变量的路径。主要是我希望语句查看驱动程序是否已安装,然后根据platform模块中的platform.system()函数检查驱动程序是否未安装,然后中断。我有以下内容,但是我遇到了一个无效的语法问题。我在windows和Linux系统上安装了这些路径,因此我知道它们可以工作 import selenium import shutil import xlsxwriter import os import unittest impor

因此,我尝试使用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")
=是赋值运算符
==是比较运算符

=用于为
变量
赋值。对于
相等性
检查
字符串
使用

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?")

使用
==
检查值是否相等<代码>=用于分配是的,我就是这么做的。对不起,我忘记登记了。