Selenium regex question - selenium

Im trying to automate the creation of a customer on the project im currently workin on, using selenium. (note, so far im only using selenium's own ide, not through java or anything else) The problem im having is that the panel i use to fill in the details is not recognised on playback of the script. This is because the panel gets a new id every time it is generated, and selenium is looking for the old one. I wanna know can i use regex somehow to match the pattern of the id instead of the actual id?
The ID looks something like this: P_0500A_72_4_P
its only the 4 that increments. and if a new browser session is started it resets to 72_0 and begins to increment again.

You have not mentioned what element the panel is. Assuming this to be a div, your html element should be something like given below
<div id="P_0500A_72_4_P">
<div id="P_0500A_72_5_P">
To identify the div use the following xpath:
"//div[contains(#id,'P_0500A_72_')]"
This xpath will select the element which has P_0500A_72_ in its ID.

Related

How to select an element from Span tag using xpath or css selector

I'm trying to automate process of pinging a person on a messenger on selecting the messenger name(Nayan)
Please find the Html code
<div class="Presence__text">
<span class="Presence__nameText">Nayan<span class="Presence__contactFlagIndicator"></span>
as text element present inside the span tag is unique (Nayan), i want to select based on that text element.
Problem statement :Unable to select an element using text present in span tag
I wanted to open the text of "Nayan" using xpath, can anyone help me solving this problem please.
XPath can be used to locate elements based on their text content.
Accordingly to presented here HTML the following XPath can be used:
"//span[contains(text(),'Nayan')]"
Selenium command using this XPath in Java (you didn't mention the language you are using, but accordingly to your previous questions I see you are using Java) can be as following:
driver.findElement(By.xpath("//span[contains(text(),'Nayan')]"));
In case "Nayan" text is unique it's always better to use contains rather to use equals method since web element may contain extra spaces.
I mean when possible [contains(text(),'Nayan')] is better to use than [text()='Nayan']
Also, since you are using Selenium it can be Xpath 1.0 only since Selenium not supporting Xpath 2.0 and higher, it supports XPath 1.0 only

Locate Tweet 'like' button using xPath (Selenium, Python)

I am trying to locate a tweet like button using xPath.
If I go to https://twitter.com/BillGates and use Chrome developer tools, I can see:
That the like button for the first tweet is located at:
/html/body/div[2]/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/div/div[2]/section/div/div/div[1]/div/div/div/div/article/div/div[2]/div[2]/div[2]/div[3]/div[3]/div/div/div[1]/svg/g/path
That the first tweet like button has:
< path d="M12 [lots of numbers]">
I tried to use both Python + Selenium and XPath Helper Chrome extension but with both approaches I cannot find the button.
I have tried without success using the full xPath and also the query below:
.find_element_by_xpath("//path[starts-with(#d, 'M')]").click()
Actually, I cannot get anything past the last div. //svg, //g and //path all get zero results. //div on the other hand, gets me 900+ results...
Appreciate if anyone can help to point me in the right direction.
Thanks!
I highly recommend not using Twitter to practice automation.
Anyways, i've seen that the likes buttons has data-testid attribute which it's value is unique to that element you are looking for, so we can easily get all the likes buttons elements like this:
//div[#data-testid="like"]
Or with css selector like this:
div[data-testid="like"]
Assuming you are writing in Python, Then we can use find_elements method to get all the elements in a list.
like_buttons = driver.find_elements_by_xpath("//div[#data-testid="like"]")
And easily click any tweet's like button you'd by index,
like_buttons[0].click() # 0 Is the first tweet, 1 is the 2nd etc...
Note that you might need to perform a hover on this element and then click it,
So if the driver find the element, but it's not clickable, it must be it.

How to update slider values?

I'd like to update valuemax value on a slider that people can only choose a value from 1 to 3, not to 5 like it is now. I have 'inspect the element' on the page, and got something like this:
<div dojoattachpoint="sliderHandle,focusNode" class="dijitSliderImageHandle dijitSliderImageHandleH" dojoattachevent="onmousedown:_onHandleClick" wairole="slider" valuemin="1" valuemax="5" role="slider" aria-valuenow="3" tabindex="0" aria-labelledby="years_label" aria-valuemin="1" aria-valuemax="5" style="position: absolute;"></div>
I'm trying to find file/place on where that value can be updated in the code, however I'm not able to figure it out.
Assuming you have access to the source code for this application, change the javascript code that creates this slider widget.
You're looking at the DOM for the page in your example. You can use the minimum and maximum properties, which translates to some of the values you see in the DOM. See the Dojo dijit/form/Slider documentation (takes you to 1.6 since it looks like you're using an older version of dojo because of the use of dojoattachpoint, those names changed in later versions of Dojo).
If you're not sure what class or widget this is in the source, look for the id element further up in the dom. Often that will have the package and class name as part of the id. Looking at the developer tools to see what files were loaded on the networking tab might give you a clue, if the application hasn't gone through any customer build or packaging to optimize it.

Retrieving a element with multiple xpaths

This isn't much of a coding question but one about what options should I take next. I've just started using selenium about a week ago and started to get the hang of most of its function.
I was working on a work project to login into various websites until I ran into a issue where I was getting this error when I tried to find the password field
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible
I went ahead and double checked the name & id but I couldn't find the field using locators like .id, name, and xpath. I inspected the xpath with Firebug and noticed that it returned two results instead of one.
Current Xpath looks like this
.//*[#id='password']
I was wondering if anyone could point me to the right direct when dealing with something like this.
Normally, id attribute suppose to be unique value, but there are some cases when front-end developer just hide part of DOM, create similar and forget to remove hidden one.
In such case you can (if it possible) ask developer to get rid of redundant code or use index as
"(.//*[#id='password'])[2]"
In cases like this you need to use another unique DOM element (parent, ancestor, sibling, child) with connection to the element you are looking for. For example In those html snippets
<div id='a'>
<div id='password'></div>
</div>
And
<div id='b'>
<div id='password'></div>
</div>
.//*[#id='password'] will have two results
However
.//*[#id='b']/[#id='password']
Will have only one result
Let me try to answer your queries one by one:
error when I tried to find the password field : This is because the property which you have mentioned id/name/css/xpath was incorrect.
ElementNotVisibleException : I doubt it was for the password_field at all. Reason behind that is, normally login field & password field stays on the same container. Either those resides on the HTML DOM (which gets loaded completely once the page loading is complete) or they may reside within a frame/iFrame (there are other measures to handle them which I am not covering here). But those 2 elements stays on the same container. So if you have found out the login_field element, password_field is just a step away.
inspected the xpath with Firebug and noticed that it returned two results : That's because the xpath which you are using doesnot identifies an unique element. There can be multiple elements on the HTML DOM with the same id attribute set to "password". But definitely the xpathof those elements will be different and unique.
What you should do?
I will suggest you the following:
a. Try to identify each element on a webpage following the attributes in sequence: id, name, visibleText, css, xpath.
b. Try to identify a unique logical xpath for elements using the other properties of the elements defined within the HTML tag. Cross check your xpath through Firebug/Firepath. There are some other handy xpath checker available too. Take help of those.
Let me know if this answers your question.

How to handle dynamic id

I am trying to explore log In button xpath with this site https://www.componence.com/login, by just recording and play back.Then I tried to get it through firepath and chrome browser default xpath copier.
But it looks like every time submit button xpath get changed with page load. I got following xpath for "Sign IN" button.
.//*[#id='yui_patched_v3_11_0_1_1487250469606_202']
.//*[#id='yui_patched_v3_11_0_1_1487251369606_202']
.//*[#id='yui_patched_v3_11_0_1_1487250229606_202']
.//*[#id='yui_patched_v3_11_0_1_1487254369606_202']
Can you please help me to retrieve correct xpath of Sign IN button which I can use with selenium IDE?
You can use below XPath to handle dynamic id:
//button[starts-with(#id, "yui_patched_v3_11_0_1_")]
But better solution is to use text content of element:
//button[normalize-space(text())="Sign In"]
I will have to disagree with the second statement of #Andersson, since it will work for .com but not for .nl.
As I see the site has a second language and my opinion is to avoid using selectors based on text on a multi-language environment.
Also as I see the id seems does not have a meaningful value, in this case try to identify a unique parent section and go from there.
One option for css/xpath would be:
css: form.sign-in-form button
xpath: //form[contains(#class, 'sign-in-form')]//button