1

我正在尝试使用 selenium 和 python 单击此单选按钮。

<input type="radio" name="tweet_target_1" value="website" class="tweet-website-button radio-selection-validate serialize-me newline-before field-order-15">

我有

website = driver.find_element(name="tweet_target_1")
website.click()

但它不允许我点击它。如何使用名称、值或类、值等的组合单击?

是否有关于如何使用硒的良好信息来源?因为我发现的大部分内容都在 java 上,而我正在使用 python。

编辑:使用 XPATH

我试过了

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()

我不断得到

AttributeError: 'list' object has no attribute 'click'

4

2 回答 2

2

我知道这可能来得太晚了,但我最近才加入。

提示:使用 Firebug 和 Firepath。找到单选按钮并找出相关元素的 xpath。

website = driver.find_element_by_xpath(".//**")
website.click()

或者

website = driver.find_element_by_xpath(".//**").click()

这应该在您尝试的所有时间都有效。此外,只需使用即可from selenium import webdriver 使该click()功能正常工作。

于 2014-05-07T17:06:24.577 回答
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.

于 2013-04-17T19:00:38.997 回答