I'm trying to extract/store href attribute from following anchor tag using xpath/css expression that can be used in selenium. Could you please shed some lights.
Bass Pro Shops® The Hunt Trophy...
For Java you would do it something like this
WebElement element = driver.findElement(By.linkPartialText("Bass Pro Shops"));
var thisLink = element.getAttribute("href");
to determine the CssSelector or Xpath we would need to see more code than just your anchor element. Something with an ID would be ideal and then all the child elements of the element with the ID down to the anchor tag
Related
Element HTML:
Inbox
What I tried:
driver.findElement(By.cssSelector("a[text='Log out']"));
then
driver.findElement(By.xpath("//a[.='Log out']"));
Element snapshot:
HTML
driver.findElement(By.linkText("Log out"));
Something like that should work, you should provide the page html for a better response.
My response is based on the fact that in your first try you are saying text='Log out'.
findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.
Let me know if it works, otherwise provide me the web page html snippet.
I've seen the actual screen, you must change Log out with Inbox
driver.findElement(By.linkText("Inbox"));
Given the HTML:
Inbox
You need to take care of a couple of things here as follows:
Within cssSelector doesn't supports the :contains("text") method
Within xpath for exact text matches you need to use text()
Solution
To identify the element you can use either of the following locator strategies:
Using linkText:
WebElement element = driver.findElement(By.linkText("Log out"));
Using cssSelector:
WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
Using xpath:
WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and #title='View the Inbox']"));
I can't get xpath of this html as it gives me error and the test case gives failure
I tried by class name of it's outer div or button
WebDriverWait wait2 = new WebDriverWait (ChromeBroswerObject, 5);
WebElement element2 = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"root\"]/div/div/div[3]/div/div[2]/section[2]/div/div[1]/a/p[1]")));
element2.sendKeys(Keys.ENTER);[enter image description here][1]
If you like to click on all shop now links, then try this,
List<WebElement> links = driver.findElements(By.xpath("//p/span[contains(.,'Shop now')]"));
for(WebElement link : links)
{
link.click();
}
Or if you like just the first shop now link,
WebElement element2 = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Epic sound for epic study sessions.')]/p/span[contains(.,'Shop now')]")));
element2.click();
It's a bit hard to tell exactly what you're trying to click, but here is my best guess:
css selector:
p.offerCta_Bis60
Xpath:
//p[#class='offerCta_Bis60']
If that's the correct element, but the class attribute changes on you, you could try this xpath:
//p and ./span[text()= 'Shop Now']
For a specific product, you can add text for that, for example this would turn the second example into this:
//a[contains(p/text(), 'Epic sound for epic study sessions.')]/p and ./span[text()= 'Shop Now']
Explanation:
You should identify some attribute you can rely on that will uniquely identify the element, preferably using an id or class.
In the second example, I'm scanning the entire document(//) looking for a p node that also has a child(./) span with the text Shop Now.
You can use this below xpath.
Make sure to replace x with the desired item number.
(//section[#data-automation='dynamic-content-offer-list']//div[contains(#class,'ItemsPerRow')])[x]
Screenshot:
If you want to get all the items in the list and then iterate through them then you can use below xpath with findElements.
//section[#data-automation='dynamic-content-offer-list']//div[contains(#class,'ItemsPerRow')]
If you want to get the paragraph under link, then simply extend the xpath as below.
(//section[#data-automation='dynamic-content-offer-list']//div[contains(#class,'ItemsPerRow')]/a/p)[1]
To click() on the element with text as Shop now as the element is a dynamic element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ta[href*='epic-sound-for-epic-study-sessions'] p[class^='offerCta_']>span"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(#href, 'epic-sound-for-epic-study-sessions')]//p[starts-with(#class, 'offer')]/span[text()='Shop now']"))).click();
First locate the parent <p> element preferably by partial text using XPath contains() function
Then locate following-sibling (next node having the same parent)
And finally filter the resulting <span> by its text
//p[contains(text(), 'Save up to')]/following-sibling::p/span[text()='Shop now']
References:
XPath Tutorial
XPath Axes
XPath Operators & Functions
i should make Selenium to click on the element of drop down menu using Java and Inteliji. I should click on the "today" button. I tried to copy the xpath, use cssselector, i used extensions like xpath finder etc, no result. The element is <li> type, so i guess the problem is here. Any suggestions how to find the correct Xpath?
P.S. sorry for uploading the image, as a new user, i can't put them exactly in the text.
Drop down menu image
html code for the elements
You can't always get reusable XPath locator for selenium from the browser's tool. It returns an absolute XPath. You need to construct relative XPath for the elements.
Here you can learn about XPath and how XPath locators work.
The following locators based on the image you have posted.
XPath:
WebElement liToday = driver.findElement(By.xpath("//div[contains(#class,'daterangepicker') and contains(#class,'dropdown-menu')]/div[#class='ranges']/ul/li[text()='Today']"));
CSS Selector:
WebElement liToday = driver.findElement(By.cssSelector("div.daterangepicker.dropdown-menu > div.ranges > ul > li"));
After locating the element,
this part is for after you have clicked the date box and the dropdown is showing.
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(liToday));
liToday.click();
<div>
<label1>Hulk</label1>
<label2>Ironman</label2>
Thor
</div>
How do I get only Thor text without using contains in xpath ?
If I try getting text of div it would give me all the 3 (Hulk, Ironman, Thor) but I want only Thor.
To extract the text Thor without using contains in xpath you can use either of the following solutions:
xpath:
String myText = driver.findElement(By.xpath("//div[not(#label)]")).getText();
cssSelector:
String myText = driver.findElement(By.cssSelector("div:not(label)")).getText();
Using JavaScriptExecutor
WebElement myElement = driver.findElement(By.xpath("//div[#attribute='value']")); //replace the pseudo attribute with actual attribute
String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString();
Note: Ensure that xpath as //div[not(#label)] and cssSelector as div:not(label) resembles to a unique element on the HTML DOM.
Could you please try the below mentioned path ?
/div/text()
I am quite new on Selenium (started today) and I would like to get the WebElement corresponding to the following html Input:
<input size="25" style="text-align:center;" value="http" onclick="this.select();" type="text"></input>
And then obtain its value. This is what I have tried so far:
WebElement element = driver.findElement(By.cssSelector(".text-align:center"));
String text = element.getText();
Or this:
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
But Java returns in both cases an exception:
org.openqa.selenium.InvalidSelectorException: The given selector
.text-align:center is either invalid or does not result in a
WebElement
Thank you,
Héctor
Do you have to search for the element by cssSelector?
You could give this a try:
WebElement element = driver.findElement(By.cssSelector("input[type='text']"));
If cssSelector is not necessary you could try grabbing the element by xpath.
If you use firefox, there is a plugin called FireBug which allows you to right click after inspecting the element and copying the xpath directly then using :
WebElement element = driver.findElement(By.xpath("XPATH HERE"));
EDIT: Part of post disappeared, redded it.
Your first try is slightly off
driver.findElement(By.cssSelector(".text-align:center"));
The (.) in a CSS selector indicates a CSS class name but that's a style on the element and not a class. There is no class on that element to use in that way.
Your second try looks good but maybe it's not unique on the page? Hard to tell with only the one line of HTML. You'd have to provide more of the HTML of the page. Try it again but get the value instead of text.
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
System.out.println(element.getAttribute("value"));
Does that work? You likely will have to provide some unique HTML that surrounds the INPUT that we can use to make the CSS selector more specific.