How to select radio button using selenium - python

I am trying to click this radio button using selenium and python.
<input type="radio" name="tweet_target_1" value="website" class="tweet-website-button radio-selection-validate serialize-me newline-before field-order-15">
I have
website = driver.find_element(name="tweet_target_1")
website.click()
but it's not allowing me to click it. How can I click using a combo of name, value or class, value etc.?
Is there a good source of info about how to use selenium? Because most of what I've found is on java and I'm using python.
EDIT: using XPATH
I tried
website = driver.find_elements(By.XPATH, "//form[#id='dmca_form' and #class='twitter-form custom-form']/div[20][#class='list-container']/div[1][#class='list-item']/div[7][#class='clearfix inf-tweet init-hide']/div[#class='input']/ul[#class='options']/li[2]/label/input[#class='tweet-website-button radio-selection-validate serialize-me newline-before field-order-15']/")
website.click()
I keep getting
AttributeError: 'list' object has no attribute 'click'

I know this comes a little too late perhaps, but I joined just recently.
Tip: Use, Firebug and with it Firepath. Locate the radio button and find out the xpath for the element in question.
website = driver.find_element_by_xpath(".//**")
website.click()
OR
website = driver.find_element_by_xpath(".//**").click()
This should work all the time you try. Also, just using from selenium import webdriver
should make the click() function work correctly.

I'm not sure where you found the documentation that said you could call find_element like that, but you should either be doing driver.find_element_by_name("tweet_target_1") or driver.find_element(By.NAME, "tweet_target_1") (having first imported By of course). Also, Selenium Java code is pretty easily convertible to Python code; it follows a few pretty simple transformation rules, and if you still have questions, all the code for the library itself will also be on your machine to look at.

Related

Can not switch to iFrame using selenium (python)

I encountered a challenge while trying to switch to a certain iFrame using selenium (in python if it matters)
I have this inner iFrame which resides in this weird HTML component
The iFrame is in this macroponent element:
and I cannot fetch it via selenium. I tried using driver.switch_to_frame("gsft_main") and also tried by using xpaths. It seems that first I need to enter this "macroponent" element before.
Any idea will be appreciated!
Refer this link for one of the example using CSS selector to access iframe in Shadow-dom:
How to access element inside shadow root in Robot framework
This link has full explanation to work with shadow-dom using python, java, javascript
https://titusfortner.com/2021/11/22/shadow-dom-selenium.html

Is there any way to click on "plain text" using selenium?

Apologies if this question was answered before, I want to click on an area in a browser with plain text using Selenium Webdriver in python
The code I'm using is:
element_plainText = driver.find_elements(By.XPATH, '//*[contains(#class, "WgFkxc")]')
element_plainText.click()
However this is returning "ElementNotInteractableException". Can anyone help me out with this?
Selenium is trying to be helpful here, by telling you why it won't click on the element; ElementNotInteractableException means it thinks that what you're trying to click on isn't clickable.
This usually happens because either:
The element isn't actually visible, or is disabled
Another element is "overlapping" the element, possibly invisibly
You're clicking something Selenium thinks won't do anything, like plain text
There's two things I'd try to get around this. Firstly, Actions. Selenium has an Action API you can use to cause specific UI events to occur. I'd suggest finding the co-ordinates of the text, then making Selenium click those co-ordinates instead of telling it to click the element. Read more about that API here.
Secondly, try clicking it with Javascript, using a Javascript Executor. That can often give you the same outcome as using Selenium directly, without it being so "helpful".

Selenium find_element_by_css_selector not finding element

I'm working on a personal project to automate the joining of a Twitch channel through Selenium using Python. This is my first time using Selenium and I've run into an issue that I can't seem to resolve.
When joining a channel that is flagged as containing mature content, it requires a user to click a button to start watching the stream. My program works great until the button needs to be clicked.
The HTML for button appears as:
<button class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jnsGXs ksFrFH" data-a-target="player-overlay-mature-accept"><div class="ScCoreButtonLabel-sc-lh1yxp-0 evtFDZ"><div data-a-target="tw-core-button-label-text" class="Layout-sc-nxg1ff-0 eZactg">Start Watching</div></div></button>
Since the class name appear to be dynamically generated, I planned on the using the "data-a-target" as the selector. Looking at Selenium's docs, it appeared that the find_element_by_css_selector function is what I need to use. I played around in the Chrome developer tools and was about to select the button using: document.querySelector('button[data-a-target="player-overlay-mature-accept"]'), so I was confident that it would work with Selenium.
I made the selection in my code with Selenium using the following code:
mature = driver.find_element_by_css_selector('button[data-a-target="player-overlay-mature-accept"]')
Unfortunately, Selenium doesn't seem to be able to find it and returns the error message:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[data-a-target="player-overlay-mature-accept"]"}
(Session info: chrome=97.0.4692.71)
Am I doing something blatantly wrong? I've spent a good amount of time trying different things but nothing seems to work.
Maybe this:
mature = driver.find_element_by_xpath("//button[#data-a-target='player-overlay-mature-accept']")
or
mature = driver.find_element_by_xpath("//button[contains(#data-a-target, 'mature')]")

Python/Selenium:Different ways to click this specific button

I am trying to understand Python in general as I just switched over from using VBA. I interested in the possible ways you could approach this single issue. I already went around it by just going to the link directly, but I need to understand and apply here.
from selenium import webdriver
chromedriver = r'C:\Users\dd\Desktop\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
url = 'https://www.fake.com/'
browser.get(url)
browser.find_element_by_id('txtLoginUserName').send_keys("Hello")
browser.find_element_by_id('txtLoginPassword').send_keys("There")
browser.find_element_by_id('btnLogin').click()
At this point, I am trying to navigate to a particular button/link.
Here is the info from the page/element
T-Mobile
Here are some of the things I tried:
for elem in browser.find_elements_by_xpath("//*[contains(text(), 'T-Mobile')]"):
elem.click
browser.execute_script("InitiateCallBack(187, True, T-Mobile, https://www.fake.com/, TMobile)")
I also attempted to look for tags and use css selector all of which I deleted out of frustration!
Specific questions
How do I utilize the innertext,"T-Mobile", to click the button?
How would I execute the onclick event?
I've tried to read the following links, but still have not succeeded incoming up with a different way. Part of it is probably because I don't understand the specific syntax yet. This is just some of the things I looked at. I spent about 3 hours trying various things before I came here!
selenium python onclick() gives StaleElementReferenceException
http://selenium-python.readthedocs.io/locating-elements.html
Python: Selenium to simulate onclick
https://stackoverflow.com/questions/43531654/simulate-a-onclick-with-selenium-https://stackoverflow.com/questions/45360707/python-selenium-using-onclick
Running javascript in Selenium using Python
How do I utilize the innertext,"T-Mobile", to click the button?
find_elements_by_link_text would be appropriate for this case.
elements = driver.find_elements_by_link_text('T-Mobile')
for elem in elements:
elem.click()
There's also a by_partial_link_text locator as well if you don't have the full exact text.
How would I execute the onclick event?
The simplest way would be to simply call .click() on the element as shown above and the event should, naturally, execute at that time.
Alternatively, you can retrieve the onclick attribute and use driver.execute_script to run the js.
for elem in elements:
script = elem.get_attribute('onlcick')
driver.execute_script(script)
Edit:
note that in your code you did element.click -- this does nothing. element.click() (note the parens) calls the click method.
is there a way to utilize browser.execute_script() for the onclick event
execute_script can fire the equivalent event, but there may be more listeners that you miss by doing this. Using the element click method is the most sound. There may very well be many implementation details of the site that may hinder your automation efforts, but those possibilities are endless. Without seeing the actual context, it's hard to say.
You can use JS methods to click an element or otherwise interact with the page, but you may miss certain event listeners that occur when using the site 'normally'; you want to emulate, more or less, the normal use as closely as possible.
As per the HTML you have shared it's pretty clear the website uses JavaScript. So to click() on the link with text as T-Mobile you have to induce WebDriverWait with expected_conditions clause as element_to_be_clickable and your can use the following code block :
WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.XPATH, "//a[contains(.,'T-Mobile')]"))).click()
you can use it
<div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">
<input class="v_small" type="button"></input>
<span>
Reset
</span>

Scroll down section of webpage with selenium/python

I'm trying to scroll down a specific div in a webpage (ticker box in facebook) using selenium (with python).
I can find the element, but when i try to scroll it down using:
header = driver.find_element_by_class_name("tickerActivityStories")
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', header)
Nothing happens.
I've found a lot of answers, but mostly are for selenium in java, so i would like to know if it's possible to do it from python.
I've found a lot of answers, but mostly are for selenium in java
There is no difference, selenium has the same api in java and python, you just need to find the same function in python selenium.
First of all check if your JS works in the browser(if not, try scroll parent element).
Also you can try :
from selenium.webdriver.common.keys import Keys
header.send_keys(Keys.PAGE_DOWN)

Categories

Resources