SyntaxError:在 Python 中使用 Selenium 使用 find_element_by_xpath 时语法无效

SyntaxError:在 Python 中使用 Selenium 使用 find_element_by_xpath 时语法无效

问题描述:

代码试用:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.niftyindices.com/reports/historical-data")
driver.maximize_window()
driver.find_element_by_xpath("//*[@id="ddlHistorical"]").send_keys("NIFTY 100")

我收到一个错误:

File "<ipython-input-32-592f058980cd>", line 5
    driver.find_element_by_xpath("//*[@id="ddlHistorical"]").send_keys("NIFTY 100")
                                                       ^
SyntaxError: invalid syntax

此错误信息...

SyntaxError: invalid syntax

...暗示 xpath 表达式不是有效的 xpath 表达式.

...implies that the xpath expression was not a valid xpath expression.

当您使用 双引号 IE "..." 作为 xpath 时,您需要提供单引号内的属性值,即'...'.

As you are using double quotes i.e. "..." for the xpath you need to provide the attribute values within single quotes i.e. '...'.

所以你需要改变:

@id="ddlHistorical"

致:

@id='ddlHistorical'

有效的代码行:

driver.find_element_by_xpath("//*[@id="ddlHistorical"]").send_keys("NIFTY 100")

将是:

driver.find_element_by_xpath("//*[@id='ddlHistorical']").send_keys("NIFTY 100")