Python selenium - clicking checkbox that also has a dropdown text - python

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

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

Selenium Python XPath Find Element that has a child/grandchild

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.

How to find the "See More Posts from..." button? Selenium - Python

Disclaimer: sorry if my explanation is weird. It's my first week learning.
As my first project, I've set out to build a program that counts Instagram posts in a given time frame. Almost everything works, but there is one button standing in my way. (sometimes)
When you open up Instagram with WebDriver, the GDPR notice pops up. This is easy to get rid of since the xpath for this button is always the same. Or less barbaric way:
driver = webdriver.Chrome()
driver.get(ig_link)
driver.find_element_by_xpath('//button[text()="Accept"]').click()
then, I want to scroll down and during it, get URLs of all posts on the page. As I've already mentioned, on some profiles this works just fine. Yet on others, only few recent posts load up and then you are required to press this pesky "Show More Posts from {instagram_tag}"
My idea was to just brute-force it through driver.find_element_by_xpath() but then I found out that on different IG pages, the xpath is different. The button also doesn't have any text directly inside it, so I am not able to use the same approach that I've used with the "Accept" button.
<button class="tCibT qq7_A z4xUb w5S7h">
<div class="Igw0E IwRSH eGOV_ _4EzTm lC6p0 HVWg4">
<div class="_7UhW9 xLCgt qyrsm h_zdq uL8Hv l4b0S ">Show More Posts from choco_afro
</div>
</div>
</button>
My idea is that I check whether the div element under button includes text "Show More Posts" and then click the button, but I don't know how to do that if even possible. Would anybody be so kind and help me with this? :)
I believe, that, this xpath might help you.
//button//div[contains(text(), 'Show More Posts')]/../..
If each IG page has the same HTML design, then this example should help you. Solution: Click Show More Posts Button
driver.find_element(By.XPATH, "//button//div[contains(text(), 'Show More Posts')]/../..").click()
Try this:
btn = driver.find_element_by_xpath('//button[#class = "tCibT qq7_A z4xUb w5S7h"]')
div = btn.find_elements_by_tag_name('div')[1]
if 'Show More Posts' in div.text.strip():
btn.click()
You can use the following to retrieve that button from it's sibling's text by css selector.
//div[text()='Show More Posts from choco_afro']/parent::div/parent::button

Python Selenium Click on buttons that doesn't have any tags/names/etc

What's the easiest way to click the buttons bellow ?
<input type="submit">
<input type="submit" value="Apply filter">
Right now, i am telling selenium to click on specific locations on the screen. In fact, it's not the best deal, because sometimes the buttons change position because of ads.
I also tried with external libraries, like pyautogui, but didn't really find any 'click' solution.
Is there a better way to detect and .click() those buttons ?
There are no. of ways to inspect the button, in your case the element just have two attributes type and value. So you may simply use the xpath to inspect the button.
Some xpaths that can be made for this element can be :
//input[#value='Apply Filter']
//input[#type='submit']
//input[text()='Apply Filter']

selenium clicking button in google translate with Robot Framework

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.

Categories

Resources