selenium clicking button in google translate with Robot Framework - python

I'm trying to use selenium to click a button in google translate (the From: button) but I'm having trouble doing so. My goal is to select a language from the drop-down menu.
This is, hopefully, the html code for the particular section that I want to click on
<div id="gt-sl-gms" class="goog-inline-block goog-flat-menu-button je" aria-expanded="false" role="listbox" style="-webkit-user-select: none;" tabindex="0" aria-haspopup="false" aria-activedescendant=""><div class="goog-inline-block goog-flat-menu-button-caption">From: English</div><div class="goog-inline-block goog-flat-menu-button-dropdown"></div></div>
Looking at this it looks like the id is gt-sl-gms so I think my selenium code should look like this
Open the Browser to google translate
Open Browser ${google_translate} ${browser}
Click From Language button
Click Button gt-sl-gms don't wait
However, this doesn't look like a traditional html button so I'm not sure this approach is the one I want.
The first test passes so I know selenium is working but the second test is failing.
Can someone please help?
Thanks

Try to use xpath and the keyword "Click Element":
Click From Language button
Click Element xpath=//div[#id="gt-sl-gms"] don't wait

I think the problem is that the 'Click Button' keyword of Robot Framework SeleniumLibrary is looking for element locators only inside <button> HTML tags. Try using the keyword 'Click Element' instead, which tries to match the locator to any HTML element regardless of their type.

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

Python selenium - clicking checkbox that also has a dropdown text

I wish to mark a checkbox, but the problem is that that text near the checkbox also incorporates a collapse-link, so whenever I try and click it, only the drop-down text is shown so the checkbox isn't being marked.
I've tried clicking the checkbox using
driver.find_element_by_xpath(".//*[contains(text(), 'example_text_positiond_next_to_checkbox')]").click()
but this, as mentioned, only clicks the text, which then proceeds to display the collapsed text
the HTML snippet is (and I hope I haven't missed any important part)
<label class="custom-control-label"
for="customCheck1"><a class="collapse-link collapsed"
data-toggle="collapse" href="#collapseInfo" role="button" aria-expanded="false"
aria-controls="collapseExample">example_text_positiond_next_to_checkbox<span
class="arrow"></span></a></label>
I've thought of somehow clicking some margin to the left of the text, but not quite sure how to do so.
any suggestions are welcomed!
that's the box:
driver.get("https://corona.health.gov.il/en/green-pass/")
driver.find_element_by_css_selector('[for="customCheck1"]').click()
input()
you can use any attribute to find element the above locator with attribute 'for' will work. You can use xpath or css
XPATH equalent:
//*[#for="customCheck1"]
This can be done with css selector, as there is no direct way to inspect that checkbox in DOM.
driver.find_element(By.CSS_SELECTOR,"input#customCheck1.custom-control-input").click()
try below css.
input#customCheck1.custom-control-input

Selenium Click JS Button - Python?

yeah, there are similar question, but upon reading through them I weren't able to find a solution for my problem.
Following situation: I'm tryin to click the "reply" button on "https://charleston.craigslist.org/ctd/d/charleston-2018-nissan-sentra-sedan-4d/7108660907.html" and after executing this click a popups shows up where I shall click another button, but let's start with the first button as the "reply" click itself is very trouble-making.
The reply button has the following X-Path:
'/html/body/section/section/header/div[2]/div/button'
speaking of which the source code is:
<button role="button" class="reply-button js-only" data-href="/__SERVICE_ID__/chs/ctd/7108660907">
reply
</button>
(see code on mentioned website).
However, my approach with Selenium (Python) doesn't work:
reply_button = '/html/body/section/section/header/div[2]/div/button'
driver.get('https://charleston.craigslist.org/ctd/d/charleston-2018-nissan-sentra-sedan-4d/7108660907.html')
driver.find_element_by_xpath(reply_button).click()
Everytime I tried, the website just loads up properly (even with implementing time.sleep(x)) and tries to click the button, but this fails and the website just refreshes - my guess is that they either regocnize the browser being Selenium-controlled, that the click isn't legitimate or that I didn't catch anything right in my code.. Anyone able to help out?
Btw I already tried searching "by_class_name", that didn't work either.
This Xpath: '/html/body/section/section/header/div[2]/div/button' is like when you get a map with instructions like step forward until you see a car then turn left 30° then step forward until you see a tree then hop twice then go to the second house to your right. Not safe to use, avoid such paths. If the page layout changes, your path may become invalid.
Try this:
button = driver.find_element_by_xpath('//*[#class="reply-button js-only"]')
button.click()
Clicking the button opens a "show phone number" popup (which may be located by driver.find_element_by_xpath('//*[#class="show-phone"]')).
Explanation:
If you want proper Xpath, inspect what you want to interact with. The button you want to click is this:
<button role="button" class="reply-button js-only" data-href="/__SERVICE_ID__/chs/ctd/7108660907">
reply
</button>
You can see that it has no "id" tag but it is a button with a specific class. You may copy right away the "class" part -> class="reply-button js-only"
Now you can check if is it unique enough:
driver.find_elements_by_xpath('//*[#class="reply-button js-only"]')
If "find elements" returns a single result, usually you should be OK. You can see that all I did is that I pasted the class inside this: driver.find_elements_by_xpath('//*[# and this ]').
If you need more accuracy, you can specify that it is a button:
driver.find_element_by_xpath('//button[#class="reply-button js-only"]')
Or it is the direct child of the element with class: class="actions-combo", so a more safe path would be:
driver.find_element_by_xpath('//*[#class="actions-combo"]/button[#class="reply-button js-only"]')
This pattern works for all webelement attributes, not just for classes. You could use the role="button" too for more filtering. Look up for Xpath, it is a pretty neat stuff.

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.

Selenium python select item in dropdown menu that is not visible and uses aria

I am using selenium with python to try and click through a dropdown menu. However the options the dropdown menu produces are not visible in the source code of the webpage.
I have not been able to find a way that lets me select an item in the dropdown by name.
So far I am able to login and click on the menu to produce the dropdown as such:
courses = driver.find_element_by_link_text("Select a course...").click()
However after this, I am not sure how to access and click the elements produced by the above click.
Here is a snippet of the HTML that activates the dropdown.
<a class="d2l-menuflyout-opener d2l-clickable" onclick="return false;" aria-haspopup="true">
<span class="d2l-menuflyout-text d2l-menuflyout-emphasis">Select a course...</span><span class="d2l-menuflyout-arrow d2l-menuflyout-arrow-open d2l_1_41_179"></span><span class="d2l-menuflyout-arrow d2l_1_40_548 d2l-menuflyout-arrow-closed"></span>
Here is a screenshot of what the menu looks like after the .click() call.
screenshot
Once the menu opens, I am not sure how to select the one I want since I don't know how the values or id's of the options are stored as it is not in the source code.
What you have shared in your html, Select a course... is not a link text it is under span tag you need to try alternative like
driver.find_element_by_xpath(".//a/span[contains(text(),'Select a course')]").click()

Categories

Resources