Python (Selenium) - 使用 Xpath 时选择器无效

标签 python html selenium xpath

我想让我的 python 单击网页中的链接,我尝试了以下 3 种方法来在我的 python 代码中为 Span 元素指定 Xpath:

driver.find_element_by_xpath("//*[@id='ChartUnitsHistory_ranges']/span[text()='1y']").click()
driver.find_element_by_xpath("//div[@class='graphControls']/span/1y")
driver.find_element_by_xpath("//a[@class='graphControls']/span[text()='1y']").click()

但所有这些都失败了,并显示相同的错误消息:

selenium.common.exceptions.InvalidSelectorException: Message: The specified selector is invalid.

更新的错误信息:

Traceback (most recent call last):   File "02042020.py", line 31, in <module>
    driver.find_element_by_xpath("//span[@id='ChartUnitsHistory_ranges']/a[text()='1y']").click() File "C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)   File "C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {   File "C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)   File "C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: The specified selector is invalid.

我需要帮助来为“1y”选项提出正确的 Xpath。

HTML 源代码:

<div class="graphControls">
            <a href="javascript:jsChartUnitsHistory.getOptions().shiftRange(100, true)">&lt;&lt;</a>&nbsp;
            <a href="javascript:jsChartUnitsHistory.getOptions().shiftRange(33, true)">&lt;</a>
            &nbsp;&nbsp;
            <a href="javascript:jsChartUnitsHistory.getOptions().shiftRange(33, false)">&gt;</a>&nbsp;
            <a href="javascript:jsChartUnitsHistory.getOptions().shiftRange(100, false)">&gt;&gt;</a>&nbsp;
            <a href="javascript:jsChartUnitsHistory.getOptions().updateRange(0,'now')">&gt;|</a>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <a href="javascript:jsChartUnitsHistory.getOptions().zoom(50);">[ + ]</a>
            <a href="javascript:jsChartUnitsHistory.getOptions().zoom(200);">[ - ]</a>
            &nbsp;&nbsp;
        <span id="ChartUnitsHistory_ranges" style="">
                    <a href="javascript:jsChartUnitsHistory.getOptions().updateRange(1,'year')">1y</a>
            <a href="javascript:jsChartUnitsHistory.getOptions().updateRange(3,'month')">3m</a>
            <a href="javascript:jsChartUnitsHistory.getOptions().updateRange(1,'month')">1m</a>
            <a href="javascript:jsChartUnitsHistory.getOptions().updateRange(2,'week')">2w</a>
            <a href="javascript:jsChartUnitsHistory.getOptions().updateRange(1,'week')">1w</a>
            <a href="javascript:jsChartUnitsHistory.getOptions().updateRange(3,'day')">3d</a>
            &nbsp;&nbsp;&nbsp;
        </span>
            <a href="#" id="ChartUnitsHistory_embiggen" onclick="EnlargeFlotChart( 'ChartUnitsHistory', jsChartUnitsHistory, 1100, 312 ); return false">enhance</a>
            <a href="#" id="ChartUnitsHistory_restore" style="display:none;" onclick="RestoreFlotChart( 'ChartUnitsHistory', jsChartUnitsHistory, 700, 160 );;return false">unenhance</a>
            <div style="clear: both;"></div>
</div>

这些元素在网页上的布局是这样的:

<< < > >> >| [+] [ - ] 1年 3米 1米 2w 1w 3d 提高 取消增强

另请参阅随附的网页截图: Screenshot of the webpage

请让我知道所提供的信息是否足够。先感谢您!

最佳答案

文本“1y”在 <a> 中标记,父元素带有 id='ChartUnitsHistory_ranges'<span>

driver.find_element_by_xpath("//span[@id='ChartUnitsHistory_ranges']/a[text()='1y']").click()

"//div[@class='graphControls']/span/1y"不起作用,因为“1y”在此处被视为标签。

"//a[@class='graphControls']/span[text()='1y']"没用因为class='graphControls'<div>标记并且该元素不是直接子元素,/是给直接 child 的,//对于任何后代。

您还可以使用 css_selector为此

driver.find_element_by_css_selector('#ChartUnitsHistory_ranges > [href$="(1,\'year\')"]').click()

关于Python (Selenium) - 使用 Xpath 时选择器无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60101574/

相关文章:

javascript - 没有路线的 Angular 5 可收藏 View 选择

python - Selenium(参数 --headless)+(木偶 = False)

java - 有没有办法先在主窗口上搜索 webelement,如果找不到,然后开始在 iframe 内搜索?

python - 如何在 <select> Python 中点击 <option>

python - 同时运行 Tkinter 窗口和 PysTray 图标

python - Django,将 HTML 代码存储在数据库中

python - 反向传播实现问题

python - 如何将用户输入存储到列表中?

javascript - 在特定条件后启用 HTML 按钮

html - 如何使两个表格水平放置在同一个html中