[英]Python (Selenium) - Invalid selector when using Xpath
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:我想让我的 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()
but all of these failed with the same error message:但所有这些都失败了,并显示相同的错误消息:
selenium.common.exceptions.InvalidSelectorException: Message: The specified selector is invalid. selenium.common.exceptions.InvalidSelectorException:消息:指定的选择器无效。
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.我需要帮助为“1y”选项提出正确的 Xpath。
HTML Source Code: 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>
The layout of these elements looks like this on the web page:这些元素的布局在网页上是这样的:
<< < > >> >| << < > >> >| [ + ] [ - ] 1y 3m 1m 2w 1w 3d enhance unenhance [ + ] [ - ] 1y 3m 1m 2w 1w 3d 增强不增强Please 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!先感谢您!
The text "1y" is in <a>
tag, the parent element with id='ChartUnitsHistory_ranges'
is <span>
文本“1y”在<a>
标签中, id='ChartUnitsHistory_ranges'
的父元素是<span>
driver.find_element_by_xpath("//span[@id='ChartUnitsHistory_ranges']/a[text()='1y']").click()
"//div[@class='graphControls']/span/1y"
didn't work because "1y" is treated like a tag here. "//div[@class='graphControls']/span/1y"
不起作用,因为“1y”在这里被视为标签。
"//a[@class='graphControls']/span[text()='1y']"
didn't work because class='graphControls'
is in <div>
tag and the element is no a direct child, /
is for direct child, //
for any descendant. "//a[@class='graphControls']/span[text()='1y']"
不起作用,因为class='graphControls'
在<div>
标签中并且元素不是直接子元素, /
用于直接子代, //
用于任何后代。
You can also use css_selector
for that您也可以css_selector
使用css_selector
driver.find_element_by_css_selector('#ChartUnitsHistory_ranges > [href$="(1,\'year\')"]').click()
The desired element is an JavaScript enabled element, so to click on the element ideally you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies :所需的元素是启用了JavaScript 的元素,因此要理想地单击该元素,您必须为element_to_be_clickable()
引入WebDriverWait ,您可以使用以下任一定位器策略:
Using LINK_TEXT
:使用LINK_TEXT
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "1y"))).click()
Using CSS_SELECTOR
:使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.graphControls span#ChartUnitsHistory_ranges a[href*='year']"))).click()
Using XPATH
:使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='graphControls']//span[@id='ChartUnitsHistory_ranges']//a[contains(@href, 'year')]"))).click()
Note : You have to add the following imports:注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
If you're using Chrome, you can click F12
to switch to the Developer Mode and locate the HTML element.如果您使用的是 Chrome,则可以单击F12
切换到开发人员模式并找到 HTML 元素。 Then right click the element to copy:然后右键单击要复制的元素:
In your case, you need to copy the Xpath
.在您的情况下,您需要复制Xpath
。 This would be a quick way to get the Xpath.这将是获取 Xpath 的快速方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.