Selenium Python XPath Find Element that has a child/grandchild - python

I'm new to Python Selenium and trying to select the ui-button tag in the following DOM structure:
...
<div>
<ui-button>
<button type='submit'>TEST</button>
</ui-button>
</div>
There's a lot of HTML in this document which I havent included, so I understand that an efficient XPath statement can accomplish this but don't know how to do it.
There's multiple ui-button tags in the page, but only one with a child button type='submit', and the goal is to click on the ui-button tag with selenium. There's no other easy way to identify these tags. So what I was doing was selecting the submit button with a css selector then using XPath to go up 1 parent, but I need to be able to select the ui-button with a single statement.
This is my best attempt:
"./ui-button/input[#type='submit']"
I'm trying to select the ui-button with a child input button that is of type 'submit'.
Thanks in advance

To select the parent ui-button node according to it's child with type='submit' you can use any of the following XPaths:
//ui-button[./button[#type='submit']]
or
//button[#type='submit']/..
or
//button[#type='submit']/../..//ui-button
But all this seems strange to me sense normally you have to click the button element, not element with strange tag name like ui-button.
However if <button type='submit'>TEST</button> is not the immediate child of the ui-button element you can use only one option from the 3 mentioned above with a slight change:
//ui-button[.//button[#type='submit']]
this means: an element with ui-button tag name somewhere on the page having somewhere inside it an element matching //button[#type='submit'] locator

In order to get the full XPath you can go to chrome and simply right-click -> Inspect to go to the chrome developer tools. Then you can right-click on the element in the HTML, go to copy, and click Copy Full XPath. Then you can use find_element_by_xpath(XPath goes here) to find the element.

Related

I cannot find a "button onclick" element using Selenium and Python

I am automating a process using Selenium and python. Right now, I am trying to click on a button in a webpage (sorry I cannot share the link, since it requires credential to login), but there is no way my code can find this button element. I have tried every selector (by id, css selector, xpath, etc.) and done a lot of googling, but no success.
Here is the source content from the web page:
<button onclick="javascript: switchTabs('public');" aria-selected="false" itemcount="-1" type="button" title="Public Reports" dontactassubmit="false" id="public" aria-label="" class="col-xs-12 text-left list-group-item tabbing_class active"> Public Reports </button>
I also added a sleep command before this to make sure the page is fully loaded, but it does not work.
Can anyone help how to select this onclick button?
Please let me know if you need more info.
Edit: you can take a look at this picture to get more insight (https://ibb.co/cYXWkL0). The yellow arrow indicates the button I want to click on.
The element you trying to click is inside an iframe. So, you need to switch driver into the iframe content before accessing elements inside it.
I can't give you a specific code solution since you didn't share a link to that page, even not all that HTML block. You can see solutions for similar questions here or enter link description here. More results can be found with google search

Unable to locate elements which really exist in Selenium

unable to locate element in selenium. that is actually a shopify App on shopify store developed using python. when ever i try to select any element on app error appearing unable to locate element
time.sleep(2)
driver.find_element_by_xpath('//*[#id="navbar"]/div/ul/li[2]/a').click()
front end code
<td class="text-start"><button type="button" class="v-btn v-btn--flat v-btn--icon v-btn--round v-btn--text theme--light v-size--default" role="button" aria-haspopup="true" aria-expanded="false"><span class="v-btn__content"><i aria-hidden="true" class="v-icon notranslate mdi mdi-dots-vertical theme--light"></i></span></button><div class="v-menu"><!----></div></td>
Trying to select this mentioned in screen shot
Don't rely on sleep(), use Waits
Do you see any <a> in your "front end code"? Neither do I. It's just impossible to select the thing that does not exists, you know :)
Do you really need your selector to be this complicated?
//*[#id="navbar"]/div/ul/li[2]/a
Or is it possible to use sort of a class or id instead?
What will happen if you won't click on a specific <a> and will try to click a whole list entry?
It's usually enough to click some outer element instead of
complecating XPath selector more and more.

How to find title="xyz" element with Selenium (Python)

I am trying to click an element with Selenium, that has a specific title attribute. I have tired do use an xpath before, however, the problem is that there are two buttons on the website with the same xpath. If one button is active, it has the same xpath as the other when its active and vice versa.
The only thing that differentiates these two buttons in the title attribute.
<a class="qPKfxd" href="SOME LINK" title="List">
Basically I am trying to only click that element if the title is "List".
Has anyone got an idea of how to specify that with Selenium?
Please let me know if you need to view more code.
You can locate an element by attribute.
xpath:
//a[#title="List"]
css_selector
[title="List"]

How to locate the element as per the html provided through Selenium, Python and Xpath

I'm using selenium to fill a webform, but this particular textbox for some reason is giving me trouble. The html for it is:
<input type="text" class="black_ar" value="" size="25" id="type" name="type"
onmouseover="showTip(this.id)" onmouseout="hideTip(this.id)"
onblur="trimByAutoTag(this);onSubTypeChangeUnit('className',this,'unitSpan')"
autocomplete="off" title="">
I have used:
driver.find_element_by_name('type').click()
and
driver.find_element_by_xpath('xpath').click()
But they both say that the element cannot be found. I am not sure if it is in an iframe or not (if anyone knows a way to check) and am using chropath which is a chrome extension to find the xpath of the element.
Edit: chropath says under the relative xpath "It might be child of svg/pseudo/comment/iframe. XPath doesn't support for them." if that helps
As per the HTML you have shared to click within the textbox you can use either of the following solutions:
xpath:
driver.find_element_by_xpath("//input[#class='black_ar' and #id='type']").click()
css_selector:
driver.find_element_by_css_selector("input.black_ar#type").click()
Now ChroPath supports iframe. So if you inspect any element and that element is inside iframe, ChroPath will tell that element is inside iframe as well as it will generate the relative xpath.
Open your page in browser let say chrome. Press right click on textbox and select inspect menu. In oped dev tools look if your textbox has parent iframe element. One way to do it:
Click inside chrome dev tools and press ctrl+f
In opened search field copy iframe input.black_ar
If does you have first switch to this frame function for switching frames in python, selenium
To locate your input use driver.find_element_by_css_selector(‘input.black_ar’)
Have you tried, driver.find_element_by_xpath("//input[#id='type']").click(); ?
You could all check by press F12 while in your browser, or going to developer tool; press Ctrl+f afterwards and type in //iframe and try to see if it exists in the page.

Python Selenium button click

I am trying to click a button to show more comments on a discusson thread on http://disqus.com/
The HTML looks like following:
<div class="load-more" data-role="more" style="">
Show more
</div>
I have tried using the xPath from the button like following:
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
driver.get(url)
driver.find_element_by_xpath('''//*[#id="posts"]/div[3]/a''').click()
But I get the NoSuchElement exception.
What is the proper way of clicking a button with that type of HTML?
UPDATE: Worked when I switched to a specific iFrame:
driver.switch_to.frame('myID')
Then loaded by class name:
element = driver.find_element_by_class_name('load-more')
element.click()
NOTE: That the click() did not work when it was performed on the same line like driver.find_element_by_class_name('load-more').click()
Xpath should be a last resort for finding elements. For your case, I would try
driver.find_element_by_class_name("load-more__button"));
Using btn load-more__button won't work as compound classNames aren't valid.
If that still doesn't work, Id recommend using css selectors before Xpath.
However, if none of these options work, and your Xpath is still not working, you should use the firebug add on for Firefox to ensure the Xpath you are using is correct.
Here's some of my other answers that may help you get started with Firebug:
https://stackoverflow.com/a/38980488/3537915
https://stackoverflow.com/a/38723782/3537915
https://stackoverflow.com/a/38744577/3537915

Categories

Resources