Selenium Webdriver:在列表中选择具有特定跨度标题的单选按钮

本文关键字:标题 单选按钮 Webdriver 列表 选择 Selenium | 更新日期: 2023-09-27 18:35:39

我有以下 HTML 在本例中在我的 ul 中有 2 个项目。现在我想检查(单击)<span title = "SeleniumUpload">的单选按钮。在此HTML中只有一个元素,但可以有更多元素。

如何找到此元素?

网页示例:

<ul id="ctl00_cpm_AlbumMain_thumbs" class="listAlbums clearfix nospace-bottom" mediastorepartnertype="extrafilm" style="height: 160px;">
<li id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle" class="folderitemhandle" pagenr="1">
    <a title="Bekijk de foto's in dit album." href="/nl/myaccount/myphotos/album.aspx?albumid=2ab03151-7f88-4924-9471-248786ba46c0">
    <img id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle_image" class="lazy" width="100" data-original="http://photos.myserver.com.stage/photos/nastemp1/volume1/8889361a-c9e5-4cbd-a5a3-bbf7612dd370/131016/4/8/48f1d74f-0e62-455a-ba60-2a64fc3ff2a6.thumb.jpg?upd=635175161317600000" alt="Bekijk de foto's in dit album." src="http://photos.myserver.com.stage/photos/nastemp1/volume1/8889361a-c9e5-4cbd-a5a3-bbf7612dd370/131016/4/8/48f1d74f-0e62-455a-ba60-2a64fc3ff2a6.thumb.jpg?upd=635175161317600000" style="display: inline;">
    </a>
    <label class="albumTitle">
        <input id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle_box" type="radio" onclick="javascript:SelectFolder('ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle','1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0');" name="ctl00$cpm$AlbumMain$1da02a93-76cd-46a5-8dfb-e841aedf4398|2ab03151-7f88-4924-9471-248786ba46c0_handle$" value="box">
        <span title="Test (2)">Test (2)</span>
    </label>
</li>
<li id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle" class="folderitemhandle" pagenr="1">
    <a title="Bekijk de foto's in dit album." href="/nl/myaccount/myphotos/album.aspx?albumid=35b9a18d-6203-4fcb-a05f-decb8672d1c7">
    <img id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle_image" class="lazy" width="100" data-original="http://photos.myserver.com.stage/photos/nastemp1/volume1/8889361a-c9e5-4cbd-a5a3-bbf7612dd370/131016/7/f/7f2efe77-7754-463e-8eb7-f2853cfa0f07.thumb.jpg?upd=635175162493870000" alt="Bekijk de foto's in dit album." src="http://photos.myserver.com.stage/photos/nastemp1/volume1/8889361a-c9e5-4cbd-a5a3-bbf7612dd370/131016/7/f/7f2efe77-7754-463e-8eb7-f2853cfa0f07.thumb.jpg?upd=635175162493870000" style="display: inline;">
    </a>
    <label class="albumTitle">
        <input id="ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle_box" type="radio" onclick="javascript:SelectFolder('ctl00_cpm_AlbumMain_1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle','1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7');" name="ctl00$cpm$AlbumMain$1da02a93-76cd-46a5-8dfb-e841aedf4398|35b9a18d-6203-4fcb-a05f-decb8672d1c7_handle$" value="box">
        <span title="SeleniumUpload (3)">Selenium... (3)</span>
    </label>
</li>

到目前为止,我已经有了这个,但是span的title属性返回一个空字符串。这是为什么呢?

var albums = driver.FindElements(By.ClassName("albumTitle")); 
Console.WriteLine("Found {0} photo-albums", albums.Count);
foreach (var we in albums)
{
var span = driver.FindElement(By.TagName("span"));
String title = span.GetAttribute("title"); // returns empty string, I need this title value
Console.WriteLine("we.text:'{0}' title:''{1}",we.Text, title);
// work around
if (we.Text.Contains("Seleni")) // I should be able to check on span title
{
  var input = we.FindElement(By.CssSelector("input[id*='ctl00_cpm_AlbumMain_']"));
  var id = input.GetAttribute("id");
  Console.WriteLine(we.Text + " Id:" + id);
}
}

Selenium Webdriver:在列表中选择具有特定跨度标题的单选按钮

您要

做的是使用xpath选择器。如果您想匹配确切的标题,则类似于//*/span[title="SeleniumUpload"]。如果您只想匹配接近它的东西,那么您需要调查使用 xpathcontains .