回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我想让我的python单击网页上的链接,我尝试了以下3种方法来在python代码中为Span元素指定Xpath:</p>
<pre><code>driver.find_element_by_xpath("//*[@id='ChartUnitsHistory_ranges']/span[text()='1y']").click()
</code></pre>
<pre><code>driver.find_element_by_xpath("//div[@class='graphControls']/span/1y")
</code></pre>
<pre><code>driver.find_element_by_xpath("//a[@class='graphControls']/span[text()='1y']").click()
</code></pre>
<p>但所有这些操作都失败,并显示相同的错误消息:</p>
<blockquote>
<p>selenium.common.exceptions.InvalidSelectorException: Message: The
specified selector is invalid.</p>
</blockquote>
<p>更新的错误消息:</p>
<pre><code>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.
</code></pre>
<p>我需要帮助为“1y”选项提供正确的Xpath</p>
<p>HTML源代码:</p>
<pre><code><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>
</code></pre>
<p>这些元素在网页上的布局如下所示:</p>
<&书信电报;
<;
&燃气轮机;
&燃气轮机&燃气轮机;
&燃气轮机|
[ + ]
[ - ]
1y
3米
1米
2w
1w
三维
增强
冷淡
<hr/>
<p>另请参见随附的网页截图:
<a href="https://i.stack.imgur.com/8iWPq.png" rel="nofollow noreferrer">Screenshot of the webpage</a></p>
<p>请告诉我提供的信息是否足够。提前谢谢你</p>