Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么每次尝试在Xpath中放入变量时都会出现语法错误_Python_Selenium_Web Scraping - Fatal编程技术网

Python 为什么每次尝试在Xpath中放入变量时都会出现语法错误

Python 为什么每次尝试在Xpath中放入变量时都会出现语法错误,python,selenium,web-scraping,Python,Selenium,Web Scraping,每次我出现语法错误时,以及当我没有出现语法错误时,Python都不会检测到变量。对字符串使用格式工具,并确保替换引号。 您可以在外面使用双色,在里面使用单色,或者在对面使用。 然后使用{}实例化变量的位置,并在字符串末尾使用.format(variable_name) def Colour(self): self.driver.find_element_by_xpath('//a[@data-style-name="'colour'"]').click() time.sleep(

每次我出现语法错误时,以及当我没有出现语法错误时,Python都不会检测到变量。

对字符串使用格式工具,并确保替换引号。 您可以在外面使用双色,在里面使用单色,或者在对面使用。 然后使用{}实例化变量的位置,并在字符串末尾使用.format(variable_name)

def Colour(self):
    self.driver.find_element_by_xpath('//a[@data-style-name="'colour'"]').click()
    time.sleep(1)

对字符串使用格式工具,并确保替换引号。 您可以在外面使用双色,在里面使用单色,或者在对面使用。 然后使用{}实例化变量的位置,并在字符串末尾使用.format(variable_name)

def Colour(self):
    self.driver.find_element_by_xpath('//a[@data-style-name="'colour'"]').click()
    time.sleep(1)

试试这个
f'//a[@data-style-name={color}]'
。注意,您需要添加
f
试试这个
f'//a[@data style name={color}]'
。请注意,您需要添加
f