Trying to find an xpath expression to use in:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPATH HERE"))).click();
The element says "Invite Users" on the page and i need to be able to click on it
I need to find the element /a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] but the problem is the characters after "inviteUsers_" is dynamically generated
I have already tried these:
"//*[contains(.,'Invite Users')]";
"//a[contains(.,'Invite Users')]";
And these give NoSuchElement exceptions.
This is the complete XPATH:
/html/body/div[#class='col-xs-10 col-xs-offset-2 main']/fieldset[#class='form-horizontal']/div[#id='roles']/div[#id='entitlements']/div[#class='panel panel-default '][3]/div[#id='service_8ef17ba4-b739-4fb6-8198-3862ea84c381']/div[#class='panel-body']/div[#class='panel panel-default'][1]/div[#class='panel-heading']/h4[#class='panel-title']/a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle']
You can solve it with starts-with():
//a[starts-with(#id, "inviteUsers_")]
If this does not work try find a unique parent.
//div[contains(#id, 'service')]//a[contains(#id, 'inviteUsers')]
Related
How to find XPath for the first searched element on given page
https://www.amazon.com/b/?encoding=UTF8&node=2346727011&bbn=7141123011&ref_=Oct_d_odnav_1040660&pd_rd_w=WJf7p&pf_rd_p=72459b27-e231-4837-b61c-b057ff0c50ac&pf_rd_r=YMKJ7M0AMXW5GER3MMRQ&pd_rd_r=eba15a0a-f59c-4dfd-a693-7c2f7f3416cf&pd_rd_wg=LRrkE
I tried the below XPath..on html page its showing the element but when putting on selenium code it's showing the error invalid selector: Unable to locate an element with the xpath expression
//div[#id='CardInstance1wPgwM8pHmoJmdnyjYOeWg']//child::div[2]//div[1]//div[1]//div[1]//div[2]//div[#class='a-section a-spacing-none a-spacing-top-small s-title-instructions-style']//span
Please help me to find xpath for first searched product.
Thanks in Advance.
You can directly use
//li[starts-with(#class,'octopus-pc-item')]
this does have 26 entries and is not unique in HTML, however, findElement will return the first matching node.
Furthermore, you can do indexing like below
(//li[starts-with(#class,'octopus-pc-item')])[1]
and [2] for the second product and so on... for other products
Perform click like below
Using ExplicitWaits
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[starts-with(#class,'octopus-pc-item')]"))).click();
Using findElement
Thread.sleep(3000);
driver.findElement(By.xpath("(//li[starts-with(#class,'octopus-pc-item')])[1]")).click();
I am trying to click on News link on google search page the HTML structure looks like this
I tried following xpaths but none worked
//a/child::span[1][contains(.,'News')]
The following xpath resulted in invalid selector: The result of the xpath expression "//a/child::span/following-sibling::text()[contains(.,'News')]" is: [object Text]. It should be an element.
//a/child::span/following-sibling::text()[contains(.,'News')]
Thanks
//a[contains(.,'News')] might return this link, but may result in a list of more than one element that you'd need to handle and select the right element from.
You can use Selenium's SearchContext to specify a container element, or solve it using an xpath one-liner like: //div[#role='navigation']//a[contains(.,'News')] (Effectively searching for a link that contains 'News' somewhere in it's html-tree, somewhere inside a div that has a role attribute with value 'navigation').
You simply need
//a[contains(., "News")]
Note that "News" is not a part of span, but a, so your 1st XPath won't work
i'm working with selenium-java to automate some tests (it's self learning). I'm stuck in click a hyperlink, but this href is pretty particular, because like this:
<a tabindex="-1" href="../../myWebPage.html"><span>My Web Page</span></a>
My java code is:
1.- driver.findElement(By.xpath("//a[#href='../../myWebPage.html']")).click();
2.- driver.findElement(By.xpath("//a[#href='https://RealHost/pag1/myWebPage.html']")).click();
The second option that i used is with the real link, but non of them is working.
Could you please help me?
P.S: I also used the option driver.findElement(By.LinkText("https://RealHost/pag1/myWebPage.html")).click(); but without success.
Thanks guys!
You should be able to locate the link element by first finding its parent element by looking through the DOM and getting the xpath for that.
Then use that parent element to find elements with a tag of "a"
WebElement parent = findElement(By.xpath("/*path to parent element here*/"));
parent.findElement(By.tagName("a")).click();
Note that the parent element may have multiple children of tagName "a" if that is the case use findElements() to get a collection of all the hyperlinks with that parent. Then search the collection for the one you want.
WebElement parent = findElements(By.xpath("/*path to parent element here*/"));
List<WebElement> elements = parent.findElements(By.tagName("a")).click();
//search the list for the correct link
Another thing you could try would be to locate the element by linkText.
findElement(By.linkText("/*The hyperlinks text*/")).click();
hope this helps!
Try either of the xpath.
driver.findElement(By.xpath("//a[contains(#href,'/myWebPage.html')]//span[text()='My Web Page']")).click();
OR
driver.findElement(By.xpath("//a[.//span[text()='My Web Page']]")).click();
OR css selector
driver.findElement(By.cssSelector("a[href*='/myWebPage.html']>span")).click();
this works for me:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("My Web Page"))).click();
i added a wait overe the element to be clickable and it's works
Thanks guys for your help.
Bye
this works fine for IE driver, but with Chrome is failing due to:
stale element reference: element is not attached to the web page
Help please!
The next command does not work
wd.findElement(By.id("post_message_*"));
The goal is to find id's with post_message_3456346, post_message_01548 and so on. How to fix it?
Actulay there is only one id on page which starts with "post_message_".
Try to locate required element by XPath as below:
wd.findElement(By.xpath("//*[starts-with(#id, 'post_message_')]"));
or by CSS selector
wd.findElement(By.cssSelector("*[id^='post_message_']"));
Try using a css Selector
wd.findElement(By.cssSelector("[id^='post_message_']"));
I need to able to click on a link (I am using Selenium and Java). I am searching the link using xpath but for some reason I am not getting most of the webpage, just a bunch of white spaces. In the image you can see the highlighted link I am looking for.
I tried:
System.out.println(driver.findElement(By.xpath("//*[#class='titre_1']/a")).getText());
System.out.println(driver.findElement(By.xpath("//*[#id='li-7']/div/a")).getText());
I get: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element
If I do:
System.out.println(driver.findElement(By.xpath("//*")).getText());
I only get a few elements from the page and bunch of white spaces. What could be wrong?
Please help. I couldn't fit in the entire html source to show you. I hope that's ok.
html source pic
In case the element is inside an iframe, you can get it by switching to iframe then call findElement. See code below:
WebElement iframeElement = driver.findElement(By.id("id_of_the_iframe"));
driver.switchTo().frame(iframeElement);
Then you can find the element with your xpath:
System.out.println(driver.findElement(By.xpath("//*[#class='titre_1']/a")).getText());