Python Selenium Package, selection of specific language - python-3.x

with selenium package for python I want to:
1. Identify element on webpage
2. Select language
I believe that presented below HTML code refers to the button I have in mind:
<ul class="nav navbar-top-links navbar-right">
<li><a>Wersja:6653.606</a></li>
<img src="Img/Flags/32/pl.png">
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Wybór języka<span class="caret"></span></a>
<ul class="dropdown-menu dropdown-user">
<li><img src="Img/Flags/16/pl.png" style="margin-right: 10px">pl</li>
<li><img src="Img/Flags/16/en.png" style="margin-right: 10px">en</li>
</ul>
</li>
<li><span class="glyphicon glyphicon-log-in"></span> Logowanie</li>
</ul>
This is dropdown list, which presents two options:
polish language
english language
How do I locate this specific object on website (dropdown list)?
I was thinking about something like:
select_element = driver.find_element_by_class_name("dropdown-menu dropdown-user")
How do I use webbrowser to select specific option? (Let's say english language)
Some sort of selection, but I have to find the element first.
Thank you.

It took me at least couple of hours to find the solution, but I did it :).
To sum up:
1. I need to select and click the listbox on website with language selection.
2. Then I need to select and click desired language.
**Solution:**
- You can find element (like listbox) with selenium on multiple different ways:
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
Documentation: https://selenium-python.readthedocs.io/locating-elements.html
- I tried multiple of them and failed.
I found info, that xpath is very reliable until somebody makes changes to html code of website.
- I have 0 knowledge of html / css etc. thus finding xpath was very difficult for me.
Fortunately I found add-in to firefox which did it for me:
https://github.com/trembacz/xpath-finder
- After installation of add-in, I first selected/found xpath for Listbox, then the same for language.
- Python code for both elements:
#Find Listbox with language selection and click it
select_element = driver.find_element_by_xpath("/html/body/form/div[3]/nav[1]/ul/li[2]/a")
select_element.click()
#Find language inside listbox and click it
select_element = driver.find_element_by_xpath("/html/body/form/div[3]/nav[1]/ul/li[2]/ul/li[2]/a")
select_element.click()
Done :)

Related

CSS selector for reCAPTCHA checkbok using Selenium and VBA Excel

On the website I am trying to fill in some fields on, there is a checkbox that I need to click to add the check mark in it:
<div class="rc-anchor-content"><div class="rc-inline-block"><div class="rc-anchor-center-container"><div class="rc-anchor-center-item rc-anchor-checkbox-holder"><span class="recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox recaptcha-checkbox-expired" role="checkbox" aria-checked="false" id="recaptcha-anchor" dir="ltr" aria-labelledby="recaptcha-anchor-label" aria-disabled="false" tabindex="0"><div class="recaptcha-checkbox-border" role="presentation" style=""></div><div class="recaptcha-checkbox-borderAnimation" role="presentation" style=""></div><div class="recaptcha-checkbox-spinner" role="presentation" style="transform: rotate(180deg);"></div><div class="recaptcha-checkbox-spinnerAnimation" role="presentation" style=""></div><div class="recaptcha-checkbox-checkmark" role="presentation"></div></span></div></div></div><div class="rc-inline-block"><div class="rc-anchor-center-container"><label class="rc-anchor-center-item rc-anchor-checkbox-label" aria-hidden="true" role="presentation" id="recaptcha-anchor-label"><span aria-live="polite" aria-labelledby="recaptcha-accessible-status"></span>I'm not a robot</label></div></div></div>
Using Selenium in VBA, I tried the following
.FindElementByCss("div.recaptcha-checkbox-border").Click
And also I tried
.FindElementByCss("span.recaptcha-checkbox").Click
But I got an error at this line.
Here's the link of the website to see the whole HTML
https://www.moj.gov.kw/AR/E-Gov/Pages/eServices01.aspx
To click() on the element, as the desired element is within an <iframe> so you have to:
Induce a waiter and switch to the desired frame.
Induce a waiter for the desired element to be clickable.
You can use the following solution:
.SwitchToFrame.FindElementByXPath("//iframe[contains(#src, 'recaptcha') and not(#title='recaptcha challenge')]", timeout:=10000)
.FindElementByCss("div.recaptcha-checkbox-checkmark").Click
You can find similar discussions in:
How to click on the reCaptcha using Selenium and Java
Find the reCAPTCHA element and click on it — Python + Selenium
Here you can find a relevant discussion on Ways to deal with #document under iframe

How to target .css selecor by text, Scrapy

I'm trying to learn Scrapy for Python(3), writing a crawler that is supposed to get data on from swedish ecommerce-site Blocket.se.
The "next page" button on the bottom of the page is one of many buttons without a unique class or id. The only difference between the buttons is the actual element text, the tags look the same.
"Next page"-button html
<a class="page_nav" itemprop="name" href="?q=macbook+air&cg=0&w=1&st=s&c=&ca=11&l=0&md=th&o=2&last=1">
Nästa sida »
</a>
"1st page"-button html
<a class="page_nav" itemprop="name" href="?q=macbook+air&cg=0&w=1&st=s&c=&ca=11&l=0&md=th">
1
</a>
Is there a way to specifically target the "next page"-button for the pagination part in the Scrapy code? Maybe by the actual text inside the element?
Try response.xpath(u'//a[contains(text(), "Nästa sida")]/#href').get()

Selenium automation - Dropdown on hover does not work

I am developing an automation script and a part of it requires me to hover over a navigation bar to display a dropdown menu. The script is written using NodeJS and the browser used is Internet Explorer.
Navigation source code
...
<ul class=navigation " data-dojo-attach-point="nonmMenu ">
<li class= "dropdown ">
<i class="fa fa-clipboard nav-icon " aria-hidden="true "></i><span>Accounts</span>
<div class='fulldrop i3">..</div>
</li>
</ul>
...
NodeJS code:
let xPathButton = "//span[text()='Accounts']";
//Find button to hover over
let buttonWithDropDown = driver.findElement(By.xpath(xPathButton));
//Hover
driver.actions().mouseMove(buttonWithDropDown).perform();
However, this does not work. The end goal is to click a link once the dropdown menu appears, which I have tried doing but as the element is not visible I get the exception ElementNotInteractableError: Cannot click on element. I would appreciate some pointers in the right direction to sort this out.
Update:
Been looking at this a bit more; Could the aria-hidden attribute in the anchor tag be causing the selenium driver to not detect the element?
Please note that changing the browser is not an option.
Try to hover over an a or li element. Also you can try click:
By.xpath("//a[span[.='Accounts']]")
By.xpath("//li[.//span[.='Accounts']]")
You can try open menu without opening menu with javascript:
executeJavaScript("arguments[0].click();", yourDropdownMenuElement);

VoiceOver: How to prevent users from accessing objects outside the menu?

If you visit www.arbetsformedlingen.se from a mobile, you will find a menu.
If you open that menu, you can only access items within that menu since tapping outside of the menu will close the menu.
If you for some reason are using a keyboard, you cannot tab out of
that menu.
However, visitors who uses the screen reader VoiceOver in IOS can simply move out of that menu by using the swipe left/right gestures to access the previous/next object in the DOM.
Question: Is there some way to prevent those users to access objects outside of the menu when the menu is visible?
An unsuitable solution due to the CMS would be to place the main content and the menu on the same node level, like in the simplified code below:
<body>
<div class=”maincontent” aria-hidden=”false”>
// Main content.
</div>
<div class=”mobilemenu” aria-hidden=”true” style="display:none">
// Menu.
</div>
</body>
When the menu is opened, the aria-hidden and display:none are toggled in order to just show the page contents or the menu.
Another unsuitable solution would be to toggle aria-hidden to every other object when the menu is opened, but that is impossible due to performance issues.
So, any piece of advice, thoughts etc are very welcome!!!
Using HTML5, you can set the "tab-index" to positive numbers on the elements within the menu. This will set focus to those elements. `
<div class="menu-container">
<div class="menu">
<div tabindex="1">Menu Item 1</div>
<div tabindex="2">Menu Item 2</div>
<div tabindex="2">Menu Item 3</div>
</div>
</div>
This may not be the best solution depending on what your trying to accomplish and what your code structure looks like.
You'll want to be sure to use the "tab-index" attribute correctly as to not break accessibility.
Good description and example
WebAIM-tabindex-accessibility

watir issue with displaying new dropdown menu

manually clicking on tab(anchor tag) its displaying drop down menu(unordered list) with watir element is locating but drop down menu is not displaying
HTML
<ul>
<li id="NetworkAnalysisTabPanel__ext-comp-1038" class=" x-tab-strip-menuable x-tab-strip-active ">
<a class="x-tab-strip-close" onclick="return false;"></a>
<a class="x-tab-strip-menu" onclick="return false;"></a>
<a></a>
<a class="x-tab-right" onclick="return false;" href=""></a>
</li>
</ul>
Tried the following line of code to click on the tab
$ff.div(:id,"NetworkAnalysisTabPanel").div(:index,1).div(:index,1).ul(:index,1).li(:index,1).link(:index,2).fire_event("onClick")
I am using watir 1.6.6 version
Firstly since your HTML sample that you provided does not include the element you are using in the command you attmepted, it's hard to know where that might be going wrong. Secondly since the code you provided does have a div with a unique ID present, why not start there instead of with an outer container.
I think the problem is that you are using
.fire_event("onCLick")
However the code is monitoring for an event named "onclick" (all lower case)
Try using
.fire_event("onclick")
or if you have not already, perhaps just
.click
and see if that works for you
Also, I'd seriously recommend you upgrade to a more current version of Watir.. 1.6.6 is pretty behind the times.
Update: that html code is starting to look very familiar to me, if this is the same basic control from the other two questions you've posted so far, then try firing the 'onmousedown' event against the element that invokes the menu and see if that works

Resources