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.
Related
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
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()
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')
I have been trying to automate my work and we use service-now for our requests. However, for the the life of me I can not get Selenium to run properly on the service-now website. It works on the login page before entering, but no matter what form of locater or x path I use it will not work. The website is dynamic so I am pretty sure xpath is needed.
I have tried directly from Google Chrome inspect as well as other xpath possibilities:
//*[#id="row_sc_request.sc_task.request_65091fb5db8163c4bc8f18df4b961921"]/td[3]/a
xpath=//a[starts-with(text(),'Open record: SCTA')]
xpath=//a[class="linked formlink" and starts-with(#aria-label='Open record: SCTA')]
This is the element copied from chrome
<a class="linked formlink" aria-label="Open record: SCTASK0067185" href="sc\\\_task.do?sys\\\_id=65091fb5db8163c4bc8f18df4b961921\\\&sysparm\\\_record\\\_target=sc\\\_task\\\&sysparm\\\_record\\\_row=1\\\&sysparm\\\_record\\\_rows=1\\\&sysparm\\\_record\\\_list=request%3D9509dbb5db8163c4bc8f18df4b96199f%5EORDERBYDESCnumber">SCTASK0067185</a>
Can someone please review my code? Any help would be appreciated!
I will suggest going with absolute XPath, I guess below note can help
Note:- if you copy XPath from Firefox it will mostly give you absolute XPath whereas chrome on another side will give relative
or the other way is to make an XPath using another stable element in the DOM tree
I hope this workout for yours. if you can share a link or inspect element snapshot showing Full DOM I can help you even better. Thanks :-)
There is a syntax error in the second xpath, this one - xpath=//a[class="linked formlink" and starts-with(aria-label='SCTA')].
In xpath the attributes must be prefixed with the "#" char; and starts-with() takes two arguments, not a boolean. So it must be:
xpath=//a[#class="linked formlink" and starts-with(#aria-label, "SCTA" )]
I don't know for sure will that make it match (it should, based on the sample), but will get you closer.
Thank you for all your answers. It turns out all I needed to do was switch the IFrame.
I am fairly new to this so I had no idea.
In my situatuion, I had to write:
"browser.switch_to.frame(browser.find_element_by_name('gsft_main'))"
where gsft_main is the name of the frame.
After doing this, then I was able to use:
browser.find_element_by_xpath("//a[#class='linked formlink' and starts-with(#aria-label, 'Open record: SCTA' )]")
and it worked out.
A solution might be to switch from an absolute xpath (the one that you find in the browser's developer tools: Copy->Copy XPath) to a relative one. For that you can install an extension like SelectorsHub and look for 'Rel XPath'
In one particular example I discovered that the absolute xpath simply did not work.
The element in this html is dynamic:
<textarea class="Medium" id="z46a662fd68e143128cd31e6978f63a5c"
name="Description" placeholder="" data-val-length="Character limit (8000)
exceeded" data-val-length-max="8000" data-val-editor-
id="z46a662fd68e143128cd31e6978f63a5c" data-val-position="0"></textarea>
I am unable to use this code in my script more than once:
driver.find_element_by_css_selector('z46a662fd68e143128cd31e6978f63a5c')
With the code below, I was able to find the element by name - but unable to use send_keys to input data.
driver.find_element_by_name('Description')
I'd like to know more than one workaround for this type of issue - Thanks in advance.
The first statement is not a proper CSS selector, hence it fails. If you wanted to choose by CSS, you could specify it like this:
driver.find_element_by_css_selector('textarea#z46a662fd68e143128cd31e6978f63a5c')
Or you could search by ID, if ID is static:
driver.find_element_by_id('z46a662fd68e143128cd31e6978f63a5c')
However it looks like your ID is dynamic, in which case looking up by name, like you did, is actually the best approach.
To send keys, you can:
driver.find_element_by_name('Description').send_keys("Hello")
If that doesn't work, make sure page loaded and textarea is rendered. You may need to use Wait:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.Name, "Description"))
element.send_keys("Hello")