硒无法通过名称或ID查找元素(Python) [英] Selenium can't find element by name or id (python)

查看:60
本文介绍了硒无法通过名称或ID查找元素(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from selenium import webdriver
import os
from selenium.webdriver import chrome

driver = webdriver.Chrome()
driver.get("http://nmcunited.me.showenter.com/%D7%9C%D7%94-%D7%9C%D7%94-%D7%9C%D7%A0%D7%93.html")
driver.implicitly_wait(15)
christmasTag = driver.find_element_by_id('f_2561406_1')
christsmasTag.click()
driver.close()

我使用上面的python代码来练习一些基本的Selenium操作. 我找不到任何元素,无论是名称还是ID.不管我尝试什么,我总是会遇到相同的错误,这表明我要查找的元素不存在. (想法是按一下其中一个按钮...)

I used the python code above in order to practice on some basic Selenium operations. I couldn't find any element, neither by name nor by id. No matter what I tried I always got the same error, which stated that the element I'm looking for doesn't exist. (the idea was to press one of the buttons, if it matters...)

这是网站html代码的一部分:

This is a part of the html code of the website:

<!-- main page -->
<td 
valign="top">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<!-- Page Content -->
<!-- Page Content -->
<tr>
<td valign="top">
<table border="0" width="100%" cellpadding="3" cellspacing="0">
<tr>
<!-- the form -->
<td valign="top">
<form id="form2" name="form2" method="post" action="/site/form/sendForm.asp" OnSubmit="return CheckSubmit();" enctype="multipart/form-data">
<!-- Form Field List -->
<table id='sop_form_594602' class='sop_all_form' align="center" border="0" width="100%" cellpadding="3" cellspacing="0" 
>
<tr>
<td style="color:;" class="changeText14">
<span style="font-weight:bold;">באיזה חג מפוטר ראין גוסלינג?</span><br>
<input type="radio" name="f_2561406" id="f_2561406_0" value="1. חג הפסחא" class="rachbox" checked>
<label for="f_2561406_0">1. חג הפסחא</label>
<br>
<input type="radio" name="f_2561406" id="f_2561406_1" value="2. חג המולד" class="rachbox">
<label for="f_2561406_1">2. חג המולד</label>
<br>
<input type="radio" name="f_2561406" id="f_2561406_2" value="3. סליבסטר" class="rachbox">
<label for="f_2561406_2">3. סליבסטר</label>
<br>
<input type="radio" name="f_2561406" id="f_2561406_3" value="4. חג ראש השנה" class="rachbox">
<label for="f_2561406_3">4. חג ראש השנה</label>
<br>
</td>
</tr>

任何帮助将不胜感激.

顺便说一句,这是我尝试运行的第一个使用Selenium的程序,是否有可能需要更改某些设置或需要安装其他设置?

By the way, this is the first program that uses Selenium I'm trying to run, is there any possibility that i need to change some settings or that i need to install something else?

推荐答案

您要查找的项目位于iframe内部.您需要先将Webdriver的上下文切换到框架.

The items you are trying to find are inside of an iframe. You need to switch the context of the webdriver to the frame first.

from selenium import webdriver
import os
from selenium.webdriver import chrome

driver = webdriver.Chrome()
driver.get("http://nmcunited.me.showenter.com/%D7%9C%D7%94-%D7%9C%D7%94-%D7%9C%D7%A0%D7%93.html")
driver.implicitly_wait(15)
frame = driver.find_element_by_css_selector('div.tool_forms iframe')
driver.switch_to.frame(frame)
christmasTag = driver.find_element_by_id('f_2561406_1')
christmasTag.click()
driver.close()

这篇关于硒无法通过名称或ID查找元素(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆