Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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-hot获取“Input”,它位于“td”中文本的前面_Python_Html_Selenium_Xpath 1.0_Xpath - Fatal编程技术网

Python Selenium-hot获取“Input”,它位于“td”中文本的前面

Python Selenium-hot获取“Input”,它位于“td”中文本的前面,python,html,selenium,xpath-1.0,xpath,Python,Html,Selenium,Xpath 1.0,Xpath,HTML: 我当前的解决方案正在工作[0]。单击=男性[1]。单击=女性 问题: 与使用[0]和[1]不同,如何设置为如果text==Male,则单击此文本之前的输入,以便此函数基于文本工作 另外,请不要建议使用M/F值,我需要使其基于文本工作 我在找这样的东西: 男性将被点击 browser.find_elements_by_xpath('//td[contains(text(),"Are you?")]')[0].find_elements_by_xpath('./following::td

HTML:

我当前的解决方案正在工作[0]。单击=男性[1]。单击=女性

问题: 与使用[0]和[1]不同,如何设置为如果text==Male,则单击此文本之前的输入,以便此函数基于文本工作

另外,请不要建议使用M/F值,我需要使其基于文本工作

我在找这样的东西:

男性将被点击

browser.find_elements_by_xpath('//td[contains(text(),"Are you?")]')[0].find_elements_by_xpath('./following::td[contains(text(),"Male")]//preceding-sibling::input')[0].click()
browser.find_elements_by_xpath('//td[contains(text(),"Are you?")]')[0].find_elements_by_xpath('./following::td[contains(text(),"Female")]//preceding-sibling::input')[0].click()
女性将被点击

browser.find_elements_by_xpath('//td[contains(text(),"Are you?")]')[0].find_elements_by_xpath('./following::td[contains(text(),"Male")]//preceding-sibling::input')[0].click()
browser.find_elements_by_xpath('//td[contains(text(),"Are you?")]')[0].find_elements_by_xpath('./following::td[contains(text(),"Female")]//preceding-sibling::input')[0].click()

您可以在xpath中使用如下内容

//td/text[contains.,'Male']/前面的兄弟姐妹::输入[1] 这将给出包含男性的文本节点,然后是相同的先前输入

另外,如果您可以使用该值,您应该使用下面的方法来选择男性选项

//td/input[@type='radio'][@value='M'] 在使用时,很难根据文本男性或女性来识别标签。或者,要单击与文本相关的元素,您需要引导WebDriverWait使元素可单击,并且您可以使用以下任意一种方法:

男:

女性:

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

请修改您的答案,因为您当前的解决方案不适用于女性。这将起作用//td/text[contains.,'Male']/previous sibling::input[1]请阅读问题,因为您的答案不依赖于文本。
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class='formtext' and contains(.,"Are you")]//following::td[1]//following::input[2]"))).click()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC