Cannot identify the object in Selenium - object

I cannot identify the object of the icon showed in the attached screen shot. I have shown the HTML code as well.
The ID's are getting changed dynamically.
Can anyone guide on how to identity this kind of objects in Selenium?

If the ID is always changed, I recommend using CssSelector instead.
For instance,
<div id="running_number_12345" class="icon something">...</div>
You can use locator
driver.FindElement(By.CssSelector("div[class*='icon something']"));
If your icon doesn't have any specific css pattern, I recommend adding something in class attribute. If not, you have to use complex CssSelector to find it.

Try this
driver.findElement(By.cssSelector(".icon something"));

Related

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.

What are alternate ways of selecting elements in python selenium?

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

How do I interact with a dynamic id through python selenium?

I am trying to click on an icon in a web page. This is the element I am attempting to click:
<a class="Button ButtonIcon IconOnly DataSelector NormalState"
id="ze6402ef81ea54445aec5dab8790c781f" tabindex="0"><span class="Icon"></span>
<span class="Text"></span></a>
I have no problem interacting with the code below:
browser.find_element_by_css_selector('ze6402ef81ea54445aec5dab8790c781f').click()
The problem is that the id is dynamic with each session. I have attempted a workaround with the following code with no success:
browser.find_element_by_xpath("//a[span/#class='Text']").click()
and
browser.find_element_by_xpath("//a[span/#class='Icon']").click()
Afterwards, I noticed that the element needs to be in a hover state in order to be clicked. So next, I used ActionChains to try to simulate a hover state -- again, with no success:
actions=ActionChains(browser)
element=browser.find_element_by_css_selector("//a[span/#class='Icon']")
actions.move_to_element(element).click().perform()
Then, I tried to TAB to the element via send_keys and ActionChains -- but it ended up cycling rapidly through page, instead of one element at a time:
actions.send_keys(Keys.TAB)
I wanted to put in my due diligence before posting my issue. Any assistance is appreciated - Thank you.
As you mentioned, you don't have a problem with the following line of code:
browser.find_element_by_css_selector('ze6402ef81ea54445aec5dab8790c781f').click()
But the only issue here is that the id is dynamic, so we can use the class attribute to construct an unique cssSelector or an unique xpath as follows:
cssSelector :
driver.findElement(By.cssSelector("div.Button.ButtonIcon.IconOnly.DataSelector.NormalState"));
xpath :
driver.findElement(By.xpath("//div[#class='Button ButtonIcon IconOnly DataSelector NormalState']"));
Use these XPaths:
//span[#class='Text']
//span[#class='Icon']
Yours were formatted incorrectly.

Selecting parent elements with WebdriverJS

I'm trying to create some scripts which require moving through a lot of on-the-fly HTML with random IDs. They require getting the parents of an element - but I'm not sure how to implement this in WebdriverJS.
If I access the element I want via a console, I can do the following to get it;
document.querySelector('span[email="noreply#example.com"]').parentNode.parentNode
Is there a way to do this in WDJS? I've looked and can't see anything obvious - it's specifically the parent stuff I'm having issue with. I saw that a possible solution may be xPath however I'm unsure of syntax having never used it before.
Thanks in advance!
I don't know the syntax of WebDriverJS. But the XPath is as below, you need a way to fit it in somewhere.
This is based on your CSS Selector, so please show HTML if needed.
.//span[#email='noreply#example.com']/../..
For example, if you have HTML like this
<div>
<div>
<span email="noreply#example.com">Contact me</span>
</div>
</div>
You can avoid using .. to go up.
.//div[./div/span[#email='noreply#example.com']]
If you have more levels to look up, another handy method would be using ancestor from XPath Axes.
Also, as #sircapsalot brought up, CSS selectors spec doesn't support parent selecting, so XPath is the only way to go, unless you inject JS.

Modx Revo Wayfinder: Add extra attribute to list item of current menu

Ref: Wayfinder on Modx Revo
I've been searching extensively and couldn't find the answer.
I find a very tricky situation trying to output an extra attribute with the list item that wraps the current menu (the menu link on of the page you're currently in).
For example:
Instead of just this line..
<li class="current">This is the menu</li>
I want..
<li class="current" value="1">This is the menu</li>
see value="1"
I tried creating a chunk in relation with the &hereTpl parameter but apparently this parameter is no longer valid in Wayfinder Revo (or is it?).
I think another possible route is if there's a way for a conditional inside the &rowTpl to render the needed attribute only for the current menu but again another brick wall.
I also tried the [[+wf.attributes]] (on the list item tag in the template chunk and put the needed attribute in the Link attributes of all the the resources/documents assuming it's going to render only when the resource/document is "current" but then the attribute is just outputted to all the menu items.
Could you share a thought? thanks so much for any help.
do the following:
[[Wayfinder? &hereTpl=`navHere` (INCLUDE OTHER PARAMETERS THAT YOU LIKE)]]
In the navHere tpl write the following piece of code:
<li class="current" value="1">[[+wf.linktext]]</li>[[+wf.wrapper]]
I understand this would work out. Hope to get a response from you.

Resources