【问题标题】:How do I find an element that contains a dynamic id and repetitive classname?如何找到包含动态 id 和重复类名的元素?
【发布时间】:2020-07-28 04:49:41
【问题描述】:

我尝试了不同的 x 路径。

//*[@id='c635_container']
//div[@id='c635_container']
(//div[@class = 'select_container'])[16]

也尝试了这些,但它选择了两条路径。

//div[label[contains(text(),'Is there an interpreter or someone else speaking on the behalf of the customer?')]]//div[@class='select-container']




//div[(label[contains(text(),'Is there an interpreter or someone else speaking on the behalf of the customer?')])[1]]

//div[label[contains(text(),'Is there an interpreter or someone else speaking on the behalf of the customer?')] and //input[@class='select2-focusser select2-offscreen']][1]

//div[@class='select-container'] and //label[contains(text(),'Is there an interpreter or someone else speaking on the behalf of the customer?')] and //input[@class='select2-focusser select2-offscreen']]

//div[label[contains(text(),'Is there an interpreter or someone else speaking on the behalf of the customer?')]]

这个

//div[@class= ‘select_container’]

是所有下拉菜单的通用 XPath,并且 id 是动态的。所以,需要另想办法。

请检查链接:-

https://drive.google.com/file/d/1a96K2Zo7wOTZIHdBXLo_z-2WSO3T0b2R/view?usp=sharing

//div//label[text()='Is there an interpreter or someone else speaking on the behalf of the customer?'] and //div[@class='select-container']

这也行不通。

【问题讨论】:

  • 可以给我们网址吗?
  • 对不起!我无法将该 URL 共享为其高度安全的站点。

标签: java selenium selenium-webdriver xpath dropdown


【解决方案1】:

尝试为需要的控制找到一些独特的。例如。只有需要的 div 包含标签,或者只有需要的 div 是具有某些独特类的 span 的子级,等等。我可以建议下一个 XPath:

//div[label[contains(text(), '<part of the text above the dropdown>')]]/div[@class = ‘select_container’]

【讨论】:

  • drive.google.com/file/d/1wMlBGDe0q1g_RXg7Jt0y1ZeakkuUtpoZ/… - 试过了,但它选择了 2 个元素 //div[label[contains(text(),'是否有口译员或其他人代表客户发言?')] 和//div[@class'select-container']]
  • 去掉'and',它应该指向正确的元素://div[label[contains(text(),'是否有口译员或其他人代表客户发言?') ]]//div[@class='select-container']
  • 试试这个://div[label[contains(text(),'有翻译或其他人代表客户说话吗?')]]//div[@class='选择容器']
  • 我试过了,还是不行。你已经在上面提到了。
  • 请检查我是否更新了问题,我还提到了我尝试过的所有路径
【解决方案2】:

答案在这里

//h1[text()=‘Interpreter’]/following-sibling::div//a[@class=‘select2-choice’]

【讨论】:

    【解决方案3】:

    试试下面的代码 - 它是手写的,所以如果有任何拼写错误,请更正它。

    //label[contains(text(),'Is there an interpreter or someone else speaking on the behalf')]/following-sibling::div[@class='select-container']/div[@class='select2-container']/input
    

    注意 - 根据需要更改目标元素。这里我使用了/input

    【讨论】:

    • 它没有选择我想要选择的下拉菜单。直到 //label[contains(text(),'Is there aninterpreter or others speak of conver')] 没问题,但在那之后,它不会选择任何下拉菜单
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多