简体   繁体   English

C# Selenium XPATH 动态按钮选择基于跨度中的文本

[英]C# Selenium XPATH Dynamic Button Select Based on Text in Span

I'm trying to select a button that is dynamically created based on the name of the person in the span and I am having trouble getting the XPATH syntax correct.我正在尝试选择一个基于跨度中人员姓名动态创建的按钮,但无法正确获取 XPATH 语法。 There would be several of these buttons dynamically created on the page so the identifier I need to use is the customer name.页面上会动态创建多个这样的按钮,因此我需要使用的标识符是客户名称。 Here is the HTML of the button.这是按钮的 HTML。

<button id="172369678903-announce" name="172369678903" data-selected-address-id="172369678903" data-unit-ids="[&quot;miq://document:1.0/Contract/a:1.0/Unit:1.0/dc290763-6cce-46c5-a878-3b5b0e615740#35176ee2-51c5-479b-b63e-a2cc958a2de9&quot;]" data-url="/spr/returns/addressSelection/dc290763-6cce-46c5-a878-3b5b0e615740" class="a-button-text selected-address" type="button">
    <div class="a-column a-span12">
        <div class="a-row">
        <div class="a-section a-spacing-none a-text-left">
            <span class="a-text-bold">
        John Doe
            </span>
        </div>
        </div>
        <div class="a-row">
        <div class="a-section a-spacing-none a-text-left">
            <span>
        20410 SOME STREET, WALNUT, CA, 91789-2435
            </span>
        </div>
        </div>
        <div class="a-row">
        <div class="a-section a-spacing-none a-text-left">
            <span>
Phone number: 2813308004
            </span>
        </div>
        </div>
    </div>
</button>

I have the information John Doe and that is how I need to be able to click on this item.我有 John Doe 的信息,这就是我需要能够点击这个项目的方式。 Here is the XPATH syntax I currently have but have tried many different forms of it.这是我目前拥有的 XPATH 语法,但已经尝试了许多不同形式的语法。 The variable shipName contains the name John Doe in it.变量 shipName 中包含名字 John Doe。

var addyFinder = driver.FindElement(By.XPath("//button/span[contains(text(),'" + shipName.Trim() + "')]"));

尝试在 XPath 下面选择所需的button节点:

"//button[normalize-space(.//span)='" + shipName.Trim() + "']"

As you mentioned there would be several of these buttons dynamically created on the page and you need to use the customer name , so you can create a function and call the function with any of the usernames as follows:正如您所提到的,页面上会动态创建几个这样的按钮,您需要使用客户名称,因此您可以创建一个函数并使用任何用户名调用该函数,如下所示:

  • Function defination:功能定义:

     public void click_user(string username) { driver.FindElement(By.XPath("//button[@class='a-button-text selected-address' and contains(@data-url,'addressSelection')]//span[@class='a-text-bold'][normalize-space()='" + username + "']")).Click(); }
  • Calling the function:调用函数:

     click_user("John Doe"); //or click_user("Roro");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM