Selenium webdriver convert CSS selector to XPath - selenium-webdriver

This is for Selenium webdriver xPath. Please refer to screen capture. I am writing following XPath for Collection #1 button, but this is just temporary text which has been given to button. but this text will be change once BLL is implemented. So how do I define XPath? In screen capture UI and HTML is there.
For now I gave following xpath in my script.
//*[#class='infix' and text()='Collection #1']
This is xPath
/html/body/esx-root/div/esx-prospect-landing/esx-community-search-hero/div[2]/div[2]/div/button[1]
This is CSS
html body esx-root div.container esx-prospect-landing.ng-star-inserted esx-community-search-hero div.content-wrapper div.bottom-wrapper div.hero-actions button span.infix
This is CSS Selector
.hero-actions > button:nth-child(1) > span:nth-child(2)
How do I define short way XPath in my script.

You should go for some tutorial to write effective and customized xpath or css path
There are lots of tutorials website you can refer as per your interest.
https://www.guru99.com/xpath-selenium.html
https://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
https://selenium-by-arun.blogspot.com/2012/12/25-locate-ui-elements-by-xpath.html
Always prefer relative xpath instead of absolute xpath and top of it, prefer CSS selector until you don't have final choice to using xpath (locate element based on text)
Now for your case you can refer below xpath to locate the element based on text
//div/button[contains(.,'Collection #1')]
OR
//button/span[class='infix'][contains(.,'Collection #1')]
And below CSS selector
.hero-actions>button span.infix
Additionally, you can take the other surrounding elements to make it unique.

Related

How to locate the XPATH of the Continue button on LinkedIn?

I right clicked the button, selected "Inspect" command and copied and used both XPATH and full XPATH. Neither of them worked for any of the commands below.
xpath = '//*[#id="ember949"]/footer/button[1]'
xpath_full = '/html/body/div[3]/div/div[2]/artdeco-tabs/artdeco-tabpanel[2]/form/footer/button[1]'
button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, xpath)))
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath_full)))
Can someone please explain? Thank you.
Without the HTML, I cannot give a definitive answer.
Looks like the first id may be dynamic and could change.
As for the second one, the full XPaths are brittle and I do not use them at all.
There could be an iframe that you should switch to first.
You could try searching the entire page for a button element with the continue-btn class:
//button[#class='continue-btn']
I would recommend using the Selector / CSS Selector. In the code just change
By.XPATH
to:
By.CSS_SELECTOR
and instead of copying the XPATH copy the Selector or in some cases CSS Selector.

"How to fix 'at org.openqa.selenium.support.ui.Select.<init>' error in selenium"

I have created the object of Select in selenium to handle dropdown . Also have included the associated packages. Yet the dropdown is not getting selected. Kindly help!
Select select = new Select(driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[1]/header[1]/div[3]/div[1]/div[1]/div[6]/ul[1]/li[1]/a[1]")));
select.selectByValue("Blouses");
I am recieving the following error "at org.openqa.selenium.support.ui.Select.(Select.java:48)";
Alongwith a note when i hover over Select -
org.openqa.selenium.support.ui.Select
Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
As far as I can see your XPath expression ends with a which indicates <a> HTML tag which in its turn stands for a hyperlink
In order to be able to use Select class you need to pass to it's constructor a WebElement instance which will point to a <select> HTML tag.
If there is no <select> elements in your page source code - it means that the dropdown is being generated by means of CSS and JavaScript therefore you just need to click the link with the Blouses text which in its turn can be as simple as:
driver.findElementByLinkText("Blouses").click()
If you still want to use XPath - be aware that you can make it a lot shorter, readable and reliable: limit your search scope to hyperlinks only like //a and utilise text() XPath function to match only "interesting" links, the expression which will click the link with Blouses text would be something like:
driver.findElementByXPath("//a[text()='Blouses']").click();

click method is not working properly in selenium webdriver

i am trying to Automate flipkart website in which i am trying to change address but "add new adress" is not getting clicked i have attached the snapshot
my code is like driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).click();
please give the appropriate help
I doesn't look that you are clicking active element, the xpath is //*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span not correct it clicks on some span.
Use Firepath https://addons.mozilla.org/en-US/firefox/addon/firepath/ to get the xpath.
To ensure that button is clickable Use isDisplayed() and isEnabled() method before clicking on "Add New Address" button, this method return boolean value.
driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).isDisplayed();
driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).isEnabled();
Also you can verify that element is exist on page or not using below code
if(driver.findElements(byVal).size()!=0){
// Element is present.
}
hope it may helpful to identify cause of issue, why its not clickable.
First and foremost, Use a customized Xpath instead of the one you are using which is extracted directly from the browser. If no customized Xpath can be constructed then try a customized Css or any other locator if possible.
Try to go through the following attempts in order (I hope you can grasp why this is):
1- If .click() still doesn’t work, then keep on changing the values of the attributes you are using to construct your customized xpath, cssSelector, or locator.
2- Use the Actions class.
3- Use JavaScript executioner
4- instead of click(), try using any of: .sendKeys(“\n”). Or .sendKeys(keys.ENTER) or .sendKeys(keys.RETURN)

Selecting Dynamic ID

<div class="html5-video-player iv-module-created iv-module-loaded endscreen-created paused-mode" tabindex="-1" id="player_uid_960859542_1" data-version="//s.ytimg.com/yts/jsbin/player-en_US-vfl_cdzrt/base.js" aria-label="YouTube Video Player">
Is there a way for me to select the above element? The id is randomly generated each time and is the css selector (#player_uid_960859542_1) and xpath (//*[#id="player_uid_960859542_1"]). I've tried using the class but it's a compound name. I tried By.cssSelector(".html5-video-player.iv-module-created.iv-module-loaded.endscreen-created.paused-mode") but that also failed.
The mentioned XPath and CSS path is not working because the value of ID is generated dynamically and both XPath and CSS are designed such that it takes ID value to generate the paths while using firebug. however you can try this XPath :
//*[#data-version='//s.ytimg.com/yts/jsbin/player-en_US-vfl_cdzrt/base.js'][#aria-label='YouTube Video Player] In case if it doesn't work you have an option to find a stable element in the DOM and then have an privilage of moving on parent node, child node or sibling node (watch the video for more detail; links are below).
Earlier I was facing the very similar issue and after a lot of R&D I found many ways to handle it using XPath and CSS selector customization.
I have made a video to get the more insights of it. Watch these two videos:
Create XPath for dynamic elements and
Create CSS path for dynamic elements
I'm sure it will help you out as there are many ways to design path to reach out to this element.

Selenium Webdriver for IE XPath - c#

Im using Selenium Webdriver for IE
I am having trouble sending a click to an image. The particular image is used throughout the page - I am attempting to find by xPath.
The html snippet:
<img src='Option.gif' border='0' style="cursor:hand;" onClick="javascript:DropDownOpen('ID_1_0_0_3_0_19');" />
The Webdriver Code:
driver.FindElement(By.XPath("//img[contains(#onClick, \"javascript:DropDownOpen('ID_1_0_0_3_0_19');\"]")).Click();
Your having a problem clicking the image because your xpath contains single quotes. To xpath to an element with single quotes you'll want to use xpath's concat() function. Your xpath becomes
//img[#onClick=concat('javascript:DropDownOpen(',"'",'ID_1_0_0_3_0_19',"'",');')]
You'll notice we concat javascript:DropDownOpen( in single quotes and anytime we need to target an element containing a single quote we wrap them in double quotes.
Happy Xpathing!

Resources