Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python/selenium单击/选择单选按钮_Python_Python 2.7_Selenium_Xpath_Radio Button - Fatal编程技术网

如何使用python/selenium单击/选择单选按钮

如何使用python/selenium单击/选择单选按钮,python,python-2.7,selenium,xpath,radio-button,Python,Python 2.7,Selenium,Xpath,Radio Button,我一直在尝试使用python/selenium从网页自动生成报告。现在我正在尝试选择一个单选按钮。你能帮我解决这个问题吗?如果可能的话,还有一些让代码看起来更好的技巧 我使用XPath Helper来识别XPath,但我得到了一个“Null”值: //*[@id="ctl00_ContentPlaceHolder1_rbUseSpecifiedDeviceConfiguration"] 这是我运行代码时收到的错误消息: Message: no such element: Unable to l

我一直在尝试使用python/selenium从网页自动生成报告。现在我正在尝试选择一个单选按钮。你能帮我解决这个问题吗?如果可能的话,还有一些让代码看起来更好的技巧

我使用XPath Helper来识别XPath,但我得到了一个“Null”值:

//*[@id="ctl00_ContentPlaceHolder1_rbUseSpecifiedDeviceConfiguration"]
这是我运行代码时收到的错误消息:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='ctl00_ContentPlaceHolder1_rbUseSpecifiedDeviceConfiguration']"}
网页代码

<div class="indent">
                    <div>
                        <input id="ctl00_ContentPlaceHolder1_rbUseDeviceDefaultConfigurations" type="radio" name="ctl00$ContentPlaceHolder1$gnConfigurationType" value="rbUseDeviceDefaultConfigurations" checked="checked"><label for="ctl00_ContentPlaceHolder1_rbUseDeviceDefaultConfigurations">Default Device Configurations</label>
                    </div>
                    <div>

                    </div>
                    <div>
                        <span class="hasValidation"><input id="ctl00_ContentPlaceHolder1_rbUseSpecifiedDeviceConfiguration" type="radio" name="ctl00$ContentPlaceHolder1$gnConfigurationType" value="rbUseSpecifiedDeviceConfiguration" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$rbUseSpecifiedDeviceConfiguration\',\'\')', 0)"><label for="ctl00_ContentPlaceHolder1_rbUseSpecifiedDeviceConfiguration">Specify Device Configuration...</label></span>
                    </div>
                    <div class="indent">

                    </div>
                    <div>

                    </div>
                    <div class="indent">

                    </div>
                </div>
import time
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys

#Opens Chrome driver and maximize window
driver = webdriver.Chrome('C:\Python27\selenium\webdriver\chrome\chromedriver.exe')
driver.maximize_window()

# Username and password for webpage
username = 'XXX'    #username to login in to web app
password = 'YYY'    #Password to login in to web app

# Gets webpage
driver.get('https://www.XXX-YYY.com/')

# Automatized script to get from main page to Trending
search_box = driver.find_element_by_xpath("//*[@id='_GlobalLoginControl_UserLogin']")   # Fills username textBox
search_box.send_keys(username)
search_box = driver.find_element_by_xpath("//*[@id='_GlobalLoginControl_Password']")    # Fills password textBox
search_box.send_keys(password)
search_box = driver.find_element_by_xpath("//*[@id='_GlobalLoginControl_LoginBtn']")    # Click login Button
search_box.click()
time.sleep(2)
search_box = driver.find_element_by_xpath("//*[@id='8:45']/ins")                        # treeBtn
search_box.click()
time.sleep(2)
search_box = driver.find_element_by_xpath("//*[@id='8:218']/ins")                       # treeBtn
search_box.click()
time.sleep(2)
search_box = driver.find_element_by_xpath("//*[@id='8:581']/ins")                       # Trending treeBtn
search_box.click()
time.sleep(2)
search_box = driver.find_element_by_xpath("//*[@id='a8:583']")                          # tag
search_box.click()
time.sleep(6)
search_box = driver.find_element_by_xpath("//*[@id='pageheading_menu_anchor']")             # Related pages dropDownList
search_box.click()
time.sleep(2)
search_box = driver.find_element_by_xpath("//*[@id='pageheading_menu_anchor_panel_s1_l4']")         # Quick trending tag
search_box.click()
time.sleep(6)
##############################
### My issue is below this ###
##############################
search_box = driver.find_element_xpath("//*[@id='ctl00_ContentPlaceHolder1_rbUseSpecifiedDeviceConfiguration']")        # Specify Device Configuration radioBtn
search_box.click()
time.sleep(2)
search_box = driver.find_element_by_xpath("//*[@id='ctl00_ContentPlaceHolder1_DeviceTrendCfgSelector_ConfigurationListAnchor']/img")        # Select Configuration dropDownList
search_box.click()
time.sleep(2)
search_box = driver.find_element_by_xpath("//*[@id='ctl00_ContentPlaceHolder1_DeviceTrendCfgSelector_ConfigurationList']/div/a[3]")         # 4 lease operator with memos and choke size
search_box.click()

在处理单选按钮之前,应切换到
iframe

driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
search_box = driver.find_element_xpath("//*[@id='ctl00_ContentPlaceHolder1_rbUseSpecifiedDeviceConfiguration']")        # Specify Device Configuration radioBtn
search_box.click()

检查单选按钮是否位于
iframe
是,它位于iframe中,然后首先需要切换到frame,然后单击单选按钮,查看信息。。。它不起作用:(…仍在寻找有关iframe的更多信息…谢谢!页面上可能有更多的
iframe
。为target
iframe
共享
HTML
我发现了问题,我使用了单引号,而不是双引号这是正确的
驱动程序。切换到.frame(driver.find\u element\u by\u标记名称(“iframe”))