How to choose a radiobuttton without id with selenium webdriver and VBA - excel

I am trying to automate the navigation of a website. I cannot choose a radio button using the methods I know so I hope someone can guide me. Here is the html:
I have tried different versions of
bot.FindElementByClass("css-k2m43g-RadioWrapper ewsdbue1").Click
bot.FindElementByClass("css-njdfi2-NativeRadio ewsdbue3").Click
bot.FindElementByClass("css-34xfl3-CustomStyledRadio ewsdbue2").Click
but that crach the VBA.
Any suggestions?

Try css attribute = value selector
bot.FindElementByCss("[type=radio]").click

Related

How to Click hidden span[role="checkbox"] in selenium

I can click hidden span tag checkbox using playwright codegen.
Here is the code:
page.frame_locator("iframe[role=\"presentation\"]").locator("span[role=\"checkbox\"]").click()
But How Can I do the same thing using selenium?
I am trying this but It's not working for me
driver.find_element(By.XPATH, "//iframe[#role='presentation']").find_element(By.XPATH, "//span[#role='checkbox']").click()
Could anyone please help me.
Thanks

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.

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.

Python 3 - Click a line in a pulldown menu only visible with mouse-over

I am trying to automate the process of logging into a website and downloading reports.
When choosing which report to download you have to mouse over a menubar on the website and hovering the mouse over the right tab will open a pulldown menu where you then click the report you want.
Screenshot of the pulldown menu (sorry, but no points embed the image :( )
I have little experience with coding and I am very new to Python 3 so I am struggling on how to do this.
Using Selenium library I am able to find things such as the user and password fields and pass in the information to automatically log into the site. But not sure how to grab or point at the correct report in this pulldown menu in order to simpy click it.
Here is a snippet of the page source... the class name = rmText... and the class name is same for all of them... only the text inside the <span> is unique but I don't know how to grab that with the Python 3 code.
Screenshot of the page source
I know of a way to do this using VB script... but I would prefer to stay with Python 3 (and also learn how to do this via Python in the process). The example below was used for a similar report where multiple lines had same class-name and we needed just the one called "Infill". An example code of the VB script:
Set project = .Document.getElementsByClassName("UPC_Items")
For Each elm In project
If instr(elm.innerhtml,"Infill") then
elm.Click
Exit For
End If
Next
How can I achieve the same using Python 3? And is Selenium the right library to use?
Thanks for reading and hopefully helping out a newbie in Python!
try below code, hope this helps:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
drive = webdriver.Firefox()
driver.get('your url')
....
steps to reach your place where that menu link is presert
....
element = driver.find_element_by_class("rmText")
putmouse = ActionChains(driver).move_to_element(element)
putmouse.perform()
time.sleep(2)
texttoclick = driver.find_element_by_xpath(".//li[#class='rmText']//span[text()='MCC']")
actions.move_to_element(texttoclick).click().perform()

Unable to click on the link using selenium and Python

Image Attached I want to click on the specific button on a webpage, using selenium webdriver and Python. Tried finding the element by CSS, class name and XPath, but it doesn't seem to work. I have attached an image showing the button I want to click (It is not a dropdown as the image suggests) and the HTML details. Any help would be appreciated.
Actually sometimes selenium is not able to interact with some web elements using click try simulating enter key press on that element for e.g. -
element = driver.find_element_by_id("value")
element.send_keys(:return)
Let me know if that works

Resources