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

问题描述 投票:0回答:2

因此,我试图做出一个if语句,以弄清楚如何动态设置硒要使用的变量的路径。最主要的是,我希望该语句查看是否已安装驱动程序,然后基于platform.system()模块的platform函数,如果未安装该驱动程序,则会中断该语句。我有以下内容,但是语法错误。我已经在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

python selenium selenium-chromedriver platform browser-automation
2个回答
0
投票

改用它:

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

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


0
投票

=用于将值分配给a variable。对于equalitystring检查,使用

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

OR

if system == 'Windows':
      browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
© www.soinside.com 2019 - 2024. All rights reserved.