XPath is not working even when directly copied from Chrome inspect - python-3.x

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.

Related

click on first search auto suggestion on website via VBA macro with selenium

I would like to know how I could program in VBA using Selenium to click/choose on website the first autosuggestion, for example as you can see in the screenshot from amazon.es
Do you have any suggestions?
Tom
Since your situation is replicable I managed to test this two (2) approaches to your situation. I'll just remind you that is always good to show us some code you've tried in your question, so it does not feel like we are helping you from scratch, also try not to rely on images to show us your situation, unless is something that does not change much «like in this case, Amazon.es page». That reminded, let's go to the good part:
1) Advanced 1:
a. Spaces in class are change for dots (if there is any)
b. Requires to understand tag's meaning (a tag is like an object)
'Example
'Clicking first element under tag ("div" alone is a tag)
Selenium.FindElementByCss("div.autocomplete-results-container > div").Click
2) Advanced 2:
a. Requires to understand what ":nth-child" (CSS selector) is
'Example:
'Clicking first child of "div" (Everything inside "div" is a child - starts in 1)
Selenium.FindElementByCss("div.autocomplete-results-container > div:nth-child(1)").Click
I used Firefox to get the xPath property of that suggestion made by the web page. To quickly compare, I copied and pasted, one at a time, the xPath of the first 3 suggestions shown:
/html/body/div[1]/header/div/div[2]/div/div[2]/div[1]/div/div[1]/span[1]
/html/body/div[1]/header/div/div[2]/div/div[2]/div[2]/div/div[1]/span[1]
/html/body/div[1]/header/div/div[2]/div/div[2]/div[3]/div/div[1]/span[1]
So, for the first item, just use the first xPath. If you want to select, for example, the second, just vary the index of the sixth DIV, as we can see in the samples above. Assuming you already have part of the code that navigates to the page, use this, adapting the name of the WebDriver:
objSeleniumDriver.FindElementByXPath("/html/body/div[1]/header/div/div[2]/div/div[2]/div[1]/div/div[1]/span[1]"). click

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()

How can I get the inspect element HTML Selenium

I'm trying to get a HTML that is not the page_source, I need the one that is generated when you open the page.
I have seen and tried all the solutions I could find but nothing really works.
I do not need any code since I do not know how to do it. I have tried with page_source but I need the inspect element HTML.
Could someone explain a good example with this?
From Get current HTML of rendered page using Selenium I suggest you using
self.driver.find_element_by_xpath("//html").get_attribute('outerHTML')

How to locate the element as per the HTML through FindElementByXPath in Selenium Basic

I'm writing a VBA code to login into a webpage and load some information i have in an excel worksheet.
I'm new in Selenium. I already got the login part right, but now i need to click in an element and i keep getting errors.
I need to click in the Company 2 button.
This is what i've got so far:
bot.FindElementByXPath("//input[#value=""Company 1""]").Click
Outputs NoSuchElementError
bot.FindElementByXPath("//input[#value=""Company 2""]").Click
Outputs ElementNotVisible
I don't know what i'm doing wrong, i think that the first input being hidden has something to do. Hope anyone can help me.
Might help you to know you can also use ByCss in most circumstances, in which case you can use:
bot.FindElementByCss("input[value='Company 1']").Click
That is nice and short.
The CSS selector is input[value='Company 1']. This says find element with input tag having attribute value with value of 'Company 1'.
XPath might be incorrect. Please try the following syntax:
FindElementByXPath("//input[#value='Company 1']")
First of all, use CSS selectors whenever possible. They are much easier to handle.
Now if you are using CSS selectors try to find the second button using something like
input[value="Company 2"]
For more info on this selector, look at https://www.w3schools.com/cssref/sel_attribute_value.asp
You can use any xpath, in first look I found your xpath is incorrect, try this:
//input[#type='button'][#value='Company 2']
//input[#type='button'&& #value='Company 2']
//input[#role='button'][#value='Company 2']
You can also use findelements() to store are all buttons and using if else you can extract the company 2 button
As per the HTML you have shared to invoke click() on the desired elements you can use the following solution:
To click on the element with text as Company 1:
bot.FindElementByXPath("//input[#class='btn_empresa ui-button ui-widget ui-state-default ui-corner-all' and #value='Company 1']").Click
To click on the element with text as Company 2:
bot.FindElementByXPath("//input[#class='btn_empresa ui-button ui-widget ui-state-default ui-corner-all' and #value='Company 2']").Click
Have you tried right-clicking the HTML in inspect and going to Copy>Copy XPath? That might give you something different. Maybe the buttons are created from Javascript and so the WebDriver can't actually see them?
Or try
Company_1 = bot.find_element_by_xpath("//input[#value='Company 1']")
Company_1.click()
Company_2 = bot.find_element_by_xpath("//input[#value='Company 2']")
Company_2.click()
And change the syntax with the ' ' quotes like someone else mentioned.

How to detect the current browser

Question: Is there a easy way to detect the browser of the user ?
Details: I would like to change for example the value of background-color of body. I've already see something like <!--[if !IE]>.
Edit:
Okay, I agree, browser detect is bad (Link taken from #TylerH), and should not be used. Thank I take note. And If someone really still want to use them, I've found a good website with a list of browser detection hack.
I think there is no way to do it with HTML or CSS (I don't like hacks).
But you could check the user agent string with JS.
See here: How to make CSS visible only for Opera

Resources