这是我的代码:
我已使用id RESULT_RadioButton-7_0的find元素,但出现以下错误:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="/home/real/Desktop/Selenium_with_python/SeleniumProjects/chromedriver_linux64/chromedriver")
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?153770259640")
radiostatus = driver.find_element(By.ID, "RESULT_RadioButton-7_0").click()
答案 0 :(得分:1)
根据您提供的页面链接,您的定位器策略似乎在这里是正确的。如果您遇到错误-很可能是NoSuchElementException
,我假设它可能与尝试找到该元素之前等待页面加载有关。让我们使用ExpectedConditions
类在元素存在之前等待它的存在:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# add the above references to your .py file
# wait on element to exist, store its reference in radiostatus
radiostatus = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "RESULT_RadioButton-7_0")))
# click the element
#radiostatus.click()
# click intercepted workaround: javascript click
driver.execute_script("arguments[0].click();", radiostatus)
这将打勾表单上“男性”旁边的单选按钮。
希望这可以解决您的错误。
答案 1 :(得分:1)
请在下面找到答案,这将帮助您单击链接中的男性单选按钮。
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
driver.get('https://fs2.formsite.com/meherpavan/form2/index.html?153770259640')
# clicking on the male checkbox button
maleRadioButton = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "RESULT_RadioButton-7_0")))
ActionChains(driver).move_to_element(maleRadioButton).click().perform()
答案 2 :(得分:-1)
除非您需要等待该元素(似乎没有必要),否则您应该能够执行以下操作:
element_to_click_or_whatever = driver.find_element_by_id('RESULT_RadioButton-7_0')
如果您查看find_element_by_id
的来源,它将以find_element
作为参数调用By.ID
:
def find_element_by_id(self, id_):
return self.find_element(by=By.ID, value=id_)
IMO:find_element_by_id
读起来更好,而且导入的包要少一个。
编辑:
我认为您的问题不是要找到要素;尝试单击元素时出现ElementClickInterceptedException
。例如,单选按钮位于,但是(奇怪地)硒不显示。
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?153770259640")
radiostatus = driver.find_element_by_id('RESULT_RadioButton-7_0')
if radiostatus:
print('found')
# found
print(radiostatus.is_displayed())
# False