Python - 如果前一个包含 xpath 中的特定类型值,则查找元素
Python - find element if the previous contains specific type value in xpath
如果前面的元素包含特定名称,我一直在努力寻找匹配元素的方法。
下面以HTML为例:
<div>
<input type="text" name="name">
</div>
<div>
<input type="text" name="email">
</div>
<div>
<input class="bla">
</div>
<!-- there might be more divs between the email / name and the button that I want to press-->
<div>
<button class="something" type="Submit"></button>
</div>
我想点击 type="submit"
的元素(而不是 button
标签,可以是任何东西)如果它在包含 name="email"
或 [=16= 的元素之后].
为了匹配 name
和 email
并用一些值完成它们,我有这个:
# some variables and booleans defined here
# (...)
driver = webdriver.Firefox()
lista = [
'https://rapidevolution.clickfunnels.com/jv-page-2',
'http://Listhubpro.com/jv',
'http://viralautopilotfunnels.com/jv',
'http://giantvideokit.com/vol3jv',
'http://www.thefivedalliance.com/jv',
'http://CPAApex.com/jv/',
'http://www.wpproassistant.com/partners/',
'http://domainerelite.com/',
'http://socialsurveys.io/jv/',
'http://tubeviperx.com/jvinvite',
'http://svensroadtoimsuccess.com/affiliates',
]
for url in lista:
not_found = False
name_required = True
email_required = True
button_required = True
driver.get(url)
time.sleep(2)
try:
name_box = driver.find_element_by_xpath("//input[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'name')]]")
name_box.click()
name_box.clear()
name_box.send_keys('MyName')
except:
not_found = True
try:
email_box = driver.find_element_by_xpath("//input[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'email')]]")
email_box.click()
email_box.clear()
email_box.send_keys('email@yahoo.com')
except:
not_found = True
if not_found:
print "here"
for element in driver.find_elements_by_xpath("//input[@type='text']"):
if name_required:
try:
name_box = element.find_element_by_xpath(".[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'name')]]")
name_box.click()
name_box.clear()
name_box.send_keys('MyName')
name_required = False
continue
except:
pass
if email_required:
try:
email_box = element.find_element_by_xpath(".[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'email')]]")
email_box.click()
email_box.clear()
email_box.send_keys('email@yahoo.com')
email_required = False
break
except:
pass
if (not name_required) and (not email_required) and (not button_required):
break
# from here, the code is not doing what I want
for element1 in driver.find_elements_by_xpath("//*[@type='submit'][preceding-sibling::email]"):
if button_required:
try:
button = element1.find_element_by_xpath("//*[@type='submit'][preceding-sibling::email]").click()
element1.click()
element1.send_keys(Keys.ENTER)
#button_required = False
continue
except:
try:
element1.find_element_by_xpath('/a').click()
#button_required = False
except:
pass
time.sleep(2)
print button_required
我恳请您的帮助,因为我已经与这种情况抗争了 10 多天。
我想你想要这样的东西:
//*[@type="Submit"][preceding::*[@name="email" or @name="name"]]
当然,如果你想让它不区分大小写地匹配,那么你需要为 "Submit"
和 "email"
和 "name"
做 translate(…)
的事情:
//*[@type[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = "submit"]][preceding::*[@name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ="email" or translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ="name"]]]
这是另一个你可以试试
//*[@name='email' or @name='name']//following::*[@type='submit']
如果前面的元素包含特定名称,我一直在努力寻找匹配元素的方法。
下面以HTML为例:
<div>
<input type="text" name="name">
</div>
<div>
<input type="text" name="email">
</div>
<div>
<input class="bla">
</div>
<!-- there might be more divs between the email / name and the button that I want to press-->
<div>
<button class="something" type="Submit"></button>
</div>
我想点击 type="submit"
的元素(而不是 button
标签,可以是任何东西)如果它在包含 name="email"
或 [=16= 的元素之后].
为了匹配 name
和 email
并用一些值完成它们,我有这个:
# some variables and booleans defined here
# (...)
driver = webdriver.Firefox()
lista = [
'https://rapidevolution.clickfunnels.com/jv-page-2',
'http://Listhubpro.com/jv',
'http://viralautopilotfunnels.com/jv',
'http://giantvideokit.com/vol3jv',
'http://www.thefivedalliance.com/jv',
'http://CPAApex.com/jv/',
'http://www.wpproassistant.com/partners/',
'http://domainerelite.com/',
'http://socialsurveys.io/jv/',
'http://tubeviperx.com/jvinvite',
'http://svensroadtoimsuccess.com/affiliates',
]
for url in lista:
not_found = False
name_required = True
email_required = True
button_required = True
driver.get(url)
time.sleep(2)
try:
name_box = driver.find_element_by_xpath("//input[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'name')]]")
name_box.click()
name_box.clear()
name_box.send_keys('MyName')
except:
not_found = True
try:
email_box = driver.find_element_by_xpath("//input[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'email')]]")
email_box.click()
email_box.clear()
email_box.send_keys('email@yahoo.com')
except:
not_found = True
if not_found:
print "here"
for element in driver.find_elements_by_xpath("//input[@type='text']"):
if name_required:
try:
name_box = element.find_element_by_xpath(".[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'name')]]")
name_box.click()
name_box.clear()
name_box.send_keys('MyName')
name_required = False
continue
except:
pass
if email_required:
try:
email_box = element.find_element_by_xpath(".[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'email')]]")
email_box.click()
email_box.clear()
email_box.send_keys('email@yahoo.com')
email_required = False
break
except:
pass
if (not name_required) and (not email_required) and (not button_required):
break
# from here, the code is not doing what I want
for element1 in driver.find_elements_by_xpath("//*[@type='submit'][preceding-sibling::email]"):
if button_required:
try:
button = element1.find_element_by_xpath("//*[@type='submit'][preceding-sibling::email]").click()
element1.click()
element1.send_keys(Keys.ENTER)
#button_required = False
continue
except:
try:
element1.find_element_by_xpath('/a').click()
#button_required = False
except:
pass
time.sleep(2)
print button_required
我恳请您的帮助,因为我已经与这种情况抗争了 10 多天。
我想你想要这样的东西:
//*[@type="Submit"][preceding::*[@name="email" or @name="name"]]
当然,如果你想让它不区分大小写地匹配,那么你需要为 "Submit"
和 "email"
和 "name"
做 translate(…)
的事情:
//*[@type[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = "submit"]][preceding::*[@name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ="email" or translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ="name"]]]
这是另一个你可以试试
//*[@name='email' or @name='name']//following::*[@type='submit']