python - 继兄弟姐妹不在 selenium python 中工作
问题描述
我想刮的部分是这样的:
<dl class="some class">
<dt> <strong>Text1</strong></dt>
<dd> Result1</dd>
<dt> <strong>Text2</strong></dt>
<dd> Result2</dd>
<dt> <strong>Text3</strong></dt>
<dd> Result3</dd>
<dt> <strong>Text4</strong></dt>
<dd> Result4</dd>
. . .
我目前正在做的是:
e = driver.find_element_by_xpath("//*[contains(text(), 'Text3')]")
当我这样做时print(e.text)
,它会成功打印Text3
. 我想要的是Result3
。当我这样做时:
driver.find_element_by_xpath("//*[contains(text(), 'Text3')]/following-sibling::dd")
这显示了一个错误NoSuchElementException
。我想要特定文本旁边的结果。
我也试过这个:
for i in driver.find_elements_by_class_name("some class"):
print(i.find_element_by_xpath("./dt[.='Text3']/following-sibling::dd").text)
仍然显示NoSuchElementException
。
解决方案
尝试以下代码以获取国家/地区名称。
parent=driver.find_element_by_css_selector("dl.BuyingOptions-labeledValues")
country=parent.find_element_by_xpath("./dt[contains(.,'Country Of Origin')]/following::dd[1]")
print(country.text)
或者
country=driver.find_element_by_xpath("//dl[contains(@class,'BuyingOptions-labeledValues')]//dt[contains(.,'Country Of Origin')]/following::dd[1]")
print(country.text)
打印:
Switzerland
推荐阅读
- scala - 模式类型与教科书示例中的预期类型不兼容
- javascript - Django Bootstrap Datepicker Plus 加载时没有默认日期或占位符
- jquery - 防止可拖动的 div 彼此靠近
- linux - 如何修复“CMakeLists.txt (bison_target_or_gen) 处的 CMake 错误:”
- reactjs - Nginx 重写代理通行证(对于节点反应应用程序)
- python - 用户输入的困难并替换字母
- javascript - 怎么修选项
- spotfire - 如何将 Spotfire 仪表板嵌入网页
- c# - 对 Unity3D 进行单元测试时,测试类看不到被测类
- typo3 - 如何配置 extbase 以获取与 tt_content 的内联/IRRE 关系?