首页 > 解决方案 > 继兄弟姐妹不在 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

标签: pythonseleniumselenium-webdriverxpath

解决方案


尝试以下代码以获取国家/地区名称。

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

推荐阅读