Selenium 无法按名称或 ID 找到元素(python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41131630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:29:07  来源:igfitidea点击:

Selenium can't find element by name or id (python)

pythonseleniumselenium-webdriver

提问by Gal Bar-nahum

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

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

我使用上面的 python 代码来练习一些基本的 Selenium 操作。我找不到任何元素,无论是按名称还是按 id。无论我尝试什么,我总是得到同样的错误,它表明我正在寻找的元素不存在。(这个想法是按下其中一个按钮,如果重要的话......)

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

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

<!-- 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>

this is the said website(broken link)

这是上述网站(断开的链接)

Any help will be appreciated.

任何帮助将不胜感激。

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?

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

回答by Lucas Tierney

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.

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

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

回答by SVS

Verify whether the "id" is getting changed and generated dynamically by refreshing the page, if it is the case then use the below xPath to find the element:

通过刷新页面验证“id”是否正在更改和动态生成,如果是,则使用以下 xPath 查找元素:

"//input[@value='2. ?? ?????']"