1

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'

0

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @grizzlyBearGrouser, one small tip you may want to format your code examples, for code blocks indent four spaces, and for inline code using backticks (e.g. code)
@robjohncox: Hey, thanks for the tip. Will keep it in mind from here on.
1

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.

7 Comments

maybe I am using a different version of selenium but it doesnt work for me i keep getting website = driver.find_element(By.NAME,"tweet_target_1") NameError: name 'By' is not defined. ALSO i need to be able to find a element by BOTH name/id and value.
Did you from selenium.webdriver.common.by import By first? And yeah, that's why there is find_element_by_name and find_element_by_id, as well as both By.NAME and By.ID. By.ID is actually the default for find_element too...
ok you were right. but is there no option for value? As you can see the above is a radio button that has only name and value. I cant select name because there are more than one option and I can't do class cause it shows up other places. Is it not an option to select by name then click by value?
You have to select the element by something, but you can use a xpath or css locator, both of which allow you to index by arbitrary attribute.
You could run it in a VM or maybe against a virtual display, but that's going to take some nontrivial and fairly advanced configuration and engineering. You also could run it with one of the headless drivers like HTMLUnit, but then you aren't really doing browser testing. So the short answer is "no".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.