I want to ask my python to click a link from a web page and I have tried below 3 ways to specify an Xpath to a Span element in my python code:
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()
but all of these failed with the same error message:
selenium.common.exceptions.InvalidSelectorException: Message: The specified selector is invalid.
Updated Error message:
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.
I need help on coming up with the correct Xpath for the '1y' option.
HTML Source Code:
<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>
The layout of these elements looks like this on the web page:
<< < > >> >| [ + ] [ - ] 1y 3m 1m 2w 1w 3d enhance unenhancePlease also see the attached screenshot of the web page: Screenshot of the webpage
Please let me know if information provided is enough or not. Thank you in advance!