gpt4 book ai didi

python - 在 python 中使用 xpath 在所有 css 选择器中搜索整个页面中的特定单词

转载 作者:行者123 更新时间:2023-12-01 04:31:47 25 4
gpt4 key购买 nike

假设我有以下 3 个链接(不过还有更多):

https://rapidevolution.clickfunnels.com/jv-page-2  
http://Listhubpro.com/jv
http://viralautopilotfunnels.com/jv

我想找到一种在这些字段中输入名称电子邮件后按下按钮的方法。

我已成功在所有页面中输入姓名电子邮件,但无缘无故无法按下按钮。要么有更多按钮,要么页面之间的 css 选择器不同。

到目前为止我的代码:

lista = [
'https://rapidevolution.clickfunnels.com/jv-page-2',
'http://Listhubpro.com/jv',
'http://viralautopilotfunnels.com/jv',
]

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_box.send_keys(Keys.Enter)
email_required = False
break
except:
pass

if (not name_required) and (not email_required) and (not button_required):
break

for element1 in driver.find_element_by_xpath("//​div​[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'button')]]"):
if button_required:
try:
button = element1.find_element_by_xpath("//*[@type='submit']").click()
button.click()
button.send_keys(Keys.ENTER)
button_required = False
continue
except:
try:
button1 = element1.find_element_by_xpath(".[@*[contains(., 'button')]]").click()
button1.click()
button1.send_keys(Keys.ENTER)
button_required = False
except:
pass

time.sleep(2)
print button_required

最佳答案

在代码第 18 行、第 26 行和第 62 行的 XPath 表达式中,有 Unicode 零宽度空格 (U+200B) 字符。您应该删除它们。

如果您将代码编辑器配置为显示非打印字符,您将看到第 18 行的代码如下所示:

name_box = driver.find_element_by_xpath("//<200b>input<200b>[@*[contains…

哪里<200b>是 Unicode 零宽度空格字符。

第 26 行和第 62 行的 XPath 表达式也是如此。因此这些 XPath 表达式永远不会匹配任何内容。请删除那些零宽度空格字符并查看您的代码是否按您预期的方式工作。

就问题中列出的文档而言,您的 XPath 表达式 //div[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'button')]]按预期工作 https://rapidevolution.clickfunnels.com/jv-page-2 。它返回 4 div元素。

关于python - 在 python 中使用 xpath 在所有 css 选择器中搜索整个页面中的特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32294854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com