<span id=":of" class="gO aQY" data-tooltip="Select Contacts" aria-label="To - Select Contacts" role="link" tabindex="1">To</span>
如何在selenium webdriver中定位元素,在这里id会随时变化。所以,我需要通过咏叹调标签“来选联系人”来定位定位selenium webdriver中的元素,如果id时时刻刻都在变化
<span id=":of" class="gO aQY" data-tooltip="Select Contacts" aria-label="To - Select Contacts" role="link" tabindex="1">To</span>
如何在selenium webdriver中定位元素,在这里id会随时变化。所以,我需要通过咏叹调标签“来选联系人”来定位定位selenium webdriver中的元素,如果id时时刻刻都在变化
与XPath得到它:
//span[@aria-label="To - Select Contacts"]
您也可以使用此XPath
//span[@data-tooltip = 'Select Contacts']
CSS选择器是我的喜欢的选择。有很多不同的属性可以用来定位你的元素而不使用它的ID。
您可以使用CSS选择器。 Smth像这样:
//If class name is permanent and there is single instance on the page
driver.findElement(By.Css(".gO aQY"))
//otherwise
driver.findElement(By.Css("span[aria-label='To - Select Contacts']"))
请参阅this链接了解更多信息。