我想让我的 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)"><<</a>
<a href="javascript:jsChartUnitsHistory.getOptions().shiftRange(33, true)"><</a>
<a href="javascript:jsChartUnitsHistory.getOptions().shiftRange(33, false)">></a>
<a href="javascript:jsChartUnitsHistory.getOptions().shiftRange(100, false)">>></a>
<a href="javascript:jsChartUnitsHistory.getOptions().updateRange(0,'now')">>|</a>
<a href="javascript:jsChartUnitsHistory.getOptions().zoom(50);">[ + ]</a>
<a href="javascript:jsChartUnitsHistory.getOptions().zoom(200);">[ - ]</a>
<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>
</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/