Python Selenium Element - Get xPath and/or CSS Selector from element (not find element by) - python-3.x

I want to get the xpath & css_selector of an element. Is there a property of the element that I can use?
Please note, I am not trying to find the an element by xpath or css selector. I already have the element, I want to know what its xpath / css_selector is.
Many thanks
I have read the documentation here: https://selenium-python.readthedocs.io/locating-elements.html... and could not find the answer.

it's not possible. you may write your own function to implement that, but there's no built-in methods. check approaches here Find an element by text and get xpath - selenium webdriver junit

Related

Selenium: failing to find element by XPATH Python

I am a little bit new to programming but python really made get into it. I am trying to create a programm that automatically checks for updates in a website. I've successfully implemented the neccessary code to call the page of enrollment but yet there is one element that cannot be located. Since I have to do it for multiple courses and iterate throught them there is no specific id, I've tried to find it by title but also this didn't work.
Is there a way you can locate the button with the title "enroll".
I've tried
driver.find_element_by_xpath("//a\[#title ='enroll']").click()
but this didn't work and I always get
NoSuchElement
error.
The XPATH for the button is simply: //*\[#id="id572"\]
Here is the part of the HTML code:
From the screenshot of HTML code you provided, the element is <button>, not <a>.
Try this xpath expression //button[#title='enroll']
This should do it if it's not in any iframes. Just grab the button whose title is enroll and click. Your css selector was an a tag and it might get id dynamically.
driver.find_element_by_css_selector("button[title ='enroll']").click()

Handling SVG elements using Karate UI Automation [duplicate]

This question already has an answer here:
Karate UI button click support
(1 answer)
Closed 2 years ago.
I am quite new to Karate UI automation and I have a front end UI that has a SVG element which when clicked brings a drop down.
When I am writing the UI test for it, I get javascript evaluation error and hence seek some advice/help.
Here are the screenshots of the UI element and its CSS locator clearly seen on the screen
This is how the CSS locator shows up for the SVG element
This uniquely identifies the SVG element(the plus button)
svg[class='svg-inline--fa fa-plus-square fa-w-14 ']
This is the part of code that I have written to click it
And click("svg[class='svg-inline--fa fa-plus-square fa-w-14 ']")
And here is the error I get:
javascript evaluation failed: click("svg[class='svg-inline--fa fa-plus-square fa-w-14 ']"), js eval failed twice:document.querySelector("svg[class='svg-inline--fa fa-plus-square fa-w-14 ']").click(), error: {"type":"object","subtype":"error","className":"TypeError","description":"TypeError: Cannot read property 'click' of null\n at <anonymous>:1:78","objectId":"{\"injectedScriptId\":2,\"id\":3}"}
I tried various things that can uniquely identify the SVG element like using the compelete Xpath and also using the parent class name(though it was already unique with just the class name) but it still did not work. I tried wildcard locators as well but since there is no text/name of the element, it did not work. When the tags are say input or button etc the same way of css locator works but SVG ones did not for me.
tagname[unique_id of the element like key=value pair]
I am wondering if we need to use a different way to identify SVG elements using Karate UI? The same path when used in Selenium worked.
Since this is a UI that requires VPN connection and secure acccess, it may not be possible to provide a minimum code to try and replicate it. But am happy to provide more details if needed.
I have many such SVG elements on my UI and any help in this would be greatly appreciated.
Use selector hub as an extension to chrome, firefox, edge and opera. Ive only used with chrome and it worked fine and allowed me to quickly and easily find svg relative / absolute xpath. This worked no problems in a find and click capacity on numerous svg elements (via karate script)
2 suggestions.
Try to get some nearby element and work backwards: https://stackoverflow.com/a/63988977/143475
Figure out the location of the SVG and fire a mouse-click: https://stackoverflow.com/a/63828083/143475
Unable to give you specifics without a way to replicate, but - it should be possible to find a "pattern" so you can write a custom function as described in the docs. So that can be your goal, something like this:
* svgClick('svg-inline--fa')

Get Selenium Element from Watir Element

This might seem like a strange request, but here goes anyhow.
Using Watir, I find an element.
Example element:
"#<Watir::Div: located: false; {:id=>\"searchFieldOptions\", :tag_name=>\"div\"}>"
One of the external tools I'm wishing to use only supports Selenium elements.
Is there a way to get a Selenium element from the Watir element?
If I find my element with: field_options = #browser.driver.find_element(:id => 'searchFieldOptions')
I can get the Selenium element:
"#<Selenium::WebDriver::Element:0x671e64fc531f7284 id=\"758f274f-c4f1-40d7-a7eb-b472705361e7\">"
I can work with this, but life would be easier if I could locate the element(s) just once.
Well, color me embarrassed.
I thought I had tried this already, but either i didn't or had a typo or?
Anyhow, once I find my element: my_watir_element.wd gives me what I need.

Node.js Selenium can't find element. No such element error

So i have this element:
and i just can't find it, tried
driver.findElement(webdriver.By.partialLinkText('my_sites')).click();
but that just throws no such element error
I believe selecting by partial link text searches the visible text (the text in between the opening and closing a tag) rather than the href. Since you have no text within the a tag, it is not finding it. You would have to find using xpath something like "//a[contains(href, 'my_sites')]"
For selenium link text is what you find between the HTML brackets, for example:
LinkText
You can try to select via CSS selector:
driver.findElement(By.cssSelector("a[href*='my_sites']")).click();
Check this link for more info:
How to click a link whose href has a certain substring in Selenium?

verify heading text in css selector

This is my xpath "//div[#class='city']/h4[text()='Newyork']"
xpaths I can use in Geb but I want to write similar expression in CSS or better Groovy-ish, Gebish locator.
I have tried
.city>h4:'Newyork'
.city>h4:contains('Newyork')
but none worked.
I have referred https://sqa.stackexchange.com/questions/362/a-way-to-match-on-text-using-css-locators
Geb has an ability to further filter down the elements matched using a CSS selector by passing a map of element attributes to its methods together with that selector. It also introduces a special attribute for matching text of a node. Your selector would look like this:
$('div.city > h4', text: 'Newyork')
Please note that this works by fetching text for every matched element and checking it against the provided value, that is it does the work on the JVM side and not in the browser, which means that you want your css selector to be as specific as possible and match as little elements as possible otherwise the selector will be very slow.

Resources