Reputation: 45
I am using Selenium and testing new things, and I tried to make a program that clicks the Like button on Yt but the XPath is wrong, and I don't know how to get the XPath that's correct, can anybody show me a way of getting the "real" Xpath of an element ?
chrome.FindElementByXPath("//*[@id='button']").Click();
There are more than one buttons using this XPath, that's for sure, but I want to know how I can specify the button I want.
<yt-icon-button id="button" class="style-scope ytd-toggle-button-renderer style-text" aria-pressed="false"><button id="button" class="style-scope yt-icon-button" aria-label="Ich mag das Video (wie 8.019 andere auch)"><yt-icon class="style-scope ytd-toggle-button-renderer"><svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" focusable="false" class="style-scope yt-icon" style="pointer-events: none; display: block; width: 100%; height: 100%;"><g class="style-scope yt-icon">
<path d="M1 21h4V9H1v12zm22-11c0-1.1-.9-2-2-2h-6.31l.95-4.57.03-.32c0-.41-.17-.79-.44-1.06L14.17 1 7.59 7.59C7.22 7.95 7 8.45 7 9v10c0 1.1.9 2 2 2h9c.83 0 1.54-.5 1.84-1.22l3.02-7.05c.09-.23.14-.47.14-.73v-1.91l-.01-.01L23 10z" class="style-scope yt-icon"></path>
</g></svg>
</yt-icon></button></yt-icon-button>
Upvotes: 0
Views: 1194
Reputation: 111686
//*[@id="button"]
There are more than one buttons using this XPath, that's for sure, but I want to know how I can specify the button I want.
There are many ways to make the above XPath be more selective. Which works best will depend upon your targeted markup, which you've neglected to show us.
Here are a few generic ideas to increase the specificity of your XPath:
/full/path/to/element[@id="button"]
//*[@id="button" and @class="UniqueClassName"]
//*[@id="button"][2]
//*[@id="button" and preceding-sibling::span="Age:"]
Upvotes: 2