【发布时间】:2020-07-22 01:51:05
【问题描述】:
我有一个非常复杂的网站,我正在尝试使用 Selenium 进行测试。但是当我尝试获取 XPath 时,例如,我会得到这样的结果。
//*[@id="datatable1595356931082"]/div[1]/div[2]/table/tbody/tr[3]/td[2]/div/select/option[8]
绝对 XPath:
/html/body/div[4]/div[2]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/table/tbody/tr[3]/td[2]/div/select/option[8]
在 Selenium 中,我尝试使用绝对路径,例如
driver.find_element_by_xpath("/html/body/div[4]/div[2]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/")
它会尝试出错,说它无法找到 XPath。数据表似乎在运行时创建了一个动态数字。 我在这里要做的是选择一个看起来像这样的下拉列表
<div role="columnheader" class="webix_hcell webix_ss_filter">
<select>
<option value=""></option>
<option value="A">A</option>
<option value="B">B</option>
.
.
.
</select>
</div>
我也试过
driver.find_element_by_class_name('webix_hcell webix_ss_filter')
但这也出错了
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".webix_hcell webix_ss_filter"}
对于上述一个
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression /html/body/div[4]/div[2]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/ because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '/html/body/div[4]/div[2]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/' is not a valid XPath expression.
(Session info: chrome=84.0.4147.89)
【问题讨论】:
标签: python selenium selenium-webdriver xpath html-select