Python 为什么每次尝试在Xpath中放入变量时都会出现语法错误
每次我出现语法错误时,以及当我没有出现语法错误时,Python都不会检测到变量。对字符串使用格式工具,并确保替换引号。 您可以在外面使用双色,在里面使用单色,或者在对面使用。 然后使用{}实例化变量的位置,并在字符串末尾使用.format(variable_name)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(
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