Reputation: 311
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. 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.
<button id="172369678903-announce" name="172369678903" data-selected-address-id="172369678903" data-unit-ids="["miq://document:1.0/Contract/a:1.0/Unit:1.0/dc290763-6cce-46c5-a878-3b5b0e615740#35176ee2-51c5-479b-b63e-a2cc958a2de9"]" 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. Here is the XPATH syntax I currently have but have tried many different forms of it. The variable shipName contains the name John Doe in it.
var addyFinder = driver.FindElement(By.XPath("//button/span[contains(text(),'" + shipName.Trim() + "')]"));
Upvotes: 2
Views: 1382
Reputation: 193208
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");
Upvotes: 0
Reputation: 52675
Try below XPath to select required button
node:
"//button[normalize-space(.//span)='" + shipName.Trim() + "']"
Upvotes: 2