找不到类名为的元素,找不到xpath为-Selenium、Python、`ERR\u SSL\u VERSION\u或\u CIPHER\u的元素`

找不到类名为的元素,找不到xpath为-Selenium、Python、`ERR\u SSL\u VERSION\u或\u CIPHER\u的元素`,python,selenium,xpath,Python,Selenium,Xpath,我需要收集的信息,从一个网页,不能被引用,因为包含成人内容。在我进入本页之前,首先,我需要单击年龄确认按钮。现在,我只对它的来源感兴趣。然而,最简单的解决方案不起作用。加载页面后,我试图找到“年龄”按钮,但收到以下消息: C:\Documents and Settings\katie>python test.py Message: {"errorMessage":"Unable to find element with class name '.enter_pl'","r equest":

我需要收集的信息,从一个网页,不能被引用,因为包含成人内容。在我进入本页之前,首先,我需要单击年龄确认按钮。现在,我只对它的来源感兴趣。然而,最简单的解决方案不起作用。加载页面后,我试图找到“年龄”按钮,但收到以下消息:

C:\Documents and Settings\katie>python test.py
Message: {"errorMessage":"Unable to find element with class name '.enter_pl'","r
equest":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Co
nnection":"close","Content-Length":"98","Content-Type":"application/json;charset
=UTF-8","Host":"127.0.0.1:1559","User-Agent":"Python-urllib/2.7"},"httpVersion":
"1.1","method":"POST","post":"{\"using\": \"class name\", \"sessionId\": \"8ef74
610-d21e-11e5-a0c9-ede1ada579b6\", \"value\": \".enter_pl\"}","url":"/element","
urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/ele
ment","relative":"/element","port":"","host":"","password":"","user":"","userInf
o":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["
element"]},"urlOriginal":"/session/8ef74610-d21e-11e5-a0c9-ede1ada579b6/element"
}}
Screenshot: available via screen
下面是代码:

#!/bin/env/python
# -*- coding: cp1250 -*-
from datetime import datetime
import time
import sys, os
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver

reload(sys)
sys.setdefaultencoding("cp1250")

main_page_url = "" # actual URL removed due to referencing adult content

def get_browser():
    return webdriver.PhantomJS("phantomjs.exe")

try :
    browser = get_browser()
    wait = WebDriverWait(browser, 30)
    browser.get(main_page_url)
    close = browser.find_element_by_class_name('.enter_pl')
    close.click()
    html = browser.page_source
    browser.close()
    print html
except Exception, e:
    print e
我还尝试使用xpath定位元素,如下所示:

#!/bin/env/python
# -*- coding: cp1250 -*-
from datetime import datetime
import time
import sys, os
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver

reload(sys)
sys.setdefaultencoding("cp1250")

main_page_url = "" # actual URL removed due to referencing adult content

def get_browser():
    return webdriver.PhantomJS("phantomjs.exe")

try :
    browser = get_browser()
    wait = WebDriverWait(browser, 30)
    browser.get(main_page_url)

    button_age_accept = browser.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div[1]/div[2]/button")
    button_age_accept.click()

    html = browser.page_source
    browser.close()
    print html
except Exception, e:
    print e
但我也收到了这个信息

C:\Documents and Settings\katie>python test2.py

Message: {"errorMessage":"Unable to find element with xpath '/html/body/div[1]/d
iv[2]/div[1]/div[1]/div[2]/button'","request":{"headers":{"Accept":"application/
json","Accept-Encoding":"identity","Connection":"close","Content-Length":"136","
Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:1655","User-Age
nt":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\":
 \"xpath\", \"sessionId\": \"63c74110-d21f-11e5-b1f9-dbb94da03942\", \"value\":
\"/html/body/div[1]/div[2]/div[1]/div[1]/div[2]/button\"}","url":"/element","url
Parsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/elemen
t","relative":"/element","port":"","host":"","password":"","user":"","userInfo":
"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["ele
ment"]},"urlOriginal":"/session/63c74110-d21f-11e5-b1f9-dbb94da03942/element"}}
Screenshot: available via screen
以下是此网页的html:

------------------------------------------------------------------编辑:我试着使用Chrome ------------------------------------------------------------------

我尝试使用chrome而不是PhantomJS来做完全一样的事情,但是chrome向我显示了ERR_SSL_VERSION_或_CIPHER_不匹配:


据我所见,按钮上的类是“关闭”而不是“输入”

在css选择器中,类名前的一个点表示类属性。当您使用find_element_by_class_name时,驱动程序正在查找以点开头的类名。您可以使用其中一种方法

browser.find_element_by_class_name('enter_pl')
# or
browser.find_element_by_css_selector('.enter_pl')
编辑

看来你要找的班级很近。试一试


你试过使用动作吗?在Java中使用WebDriver

 Actions act=new Actions(driver);
 act.moveToElement(driver.findElement(By.className(".enter_pl"))).click().build().perform();
大多数情况下,如果元素是可直接查找的,则moveToElement对我有效

另外,我们尝试通过指向最短的特定工作路径来代替绝对xpath/html/body/div[1]/div[2]/button,例如//div[1]/div[2]/button,如果这个最短xpath没有获取已知的必需元素,我们可以向它添加另一个父级

在这里,我们还可以使用Javascriptexecutor单击按钮

 WebElement element = driver.findElement(By.className(".enter_pl"));
 JavascriptExecutor executor = (JavascriptExecutor)driver;
 executor.executeScript("arguments[0].click();", element);
我只是尝试了更好的方法来处理java+webdriver,我使用了java+webdriver,我希望您可以尝试使用相同的逻辑和准确的定位器,因为HTML代码并没有提供,并且根据问题,类不起作用

谢谢,,
Murali

我昨天看到了这个,下面的代码适合我,使用xpath./*[@id='confirmage']]/div[2]/按钮

在Java中:注:如果您需要,我可以给您一个python代码

driver.get("url");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[@id='confirmage']/div[2]/button")).click();
这个python脚本对我来说很好

driver.get("you url")
driver.maximize_window()
wait = WebDriverWait(driver, 20)
# wait for the page to load
wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id='confirmage']/div[2]/button")))
elemnt =driver.find_element_by_xpath(".//*[@id='confirmage']/div[2]/button")
elemnt.click();
使用按类名称查找元素时,不应在类名前加上点:

driver.find_element_by_class_name("enter_pl")
如果使用CSS选择器,则需要点:

driver.find_element_by_css_selector(".enter_pl")
您可能还需要,然后单击:

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

wait = WebDriverWait(driver, 10)
age = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".enter_pl")))
age.click()
另外,检查元素是否在iframe中。如果是,则需要在搜索元素之前切换到帧的上下文:

driver.switch_to.frame("frame_name_or_id")
driver.find_element_by_class_name("enter_pl").click()

PhantomJS似乎返回了一个空的页面源。如果您添加一个忽略ssl错误的标志,可能会有所帮助

def get_browser():
    return webdriver.PhantomJS('phantomjs.exe', service_args=['--ignore-ssl-errors=true', '–ssl-protocol=any'])

你链接到的网页似乎是色情的。我在等待主持人的介入。@R.穆雷:真的吗?这是禁止的吗?我想是的。我在这里勾选了:这个问题必须被删除或编辑,这样它就不会引用成人网站。引用:包括成人链接,甚至提到成人网站的名称,是明确不允许的@KatieHmm很有趣,但将其更改为close会导致找不到类名为“close”的元素——此外,xpath方法不使用该名称,但是,不起作用well@Katie您是否尝试过使用close类名,而不是使用我的答案中的解决方案输入\u pl?谢谢,这些都不行。它告诉我:无法使用css选择器找到元素“.enter_pl”或无法找到类名为“enter_pl”的元素…@Katie你能添加html吗?这将更容易帮助您。@Katie当您尝试@noob解决方案时,您是尝试按类名称“close”查找元素还是按类名称“close”查找元素。在关闭前用点“close”查找元素?两种方法都试过了。他们都没有工作。当然,我会编辑我的第一篇文章并粘贴html。不幸的是,没有。它让我无法找到xpath元素…我看到了你的脚本让我试试它在Java中对我很好我已经添加了python脚本它对我很好让我知道你是否仍然有问题?它只给了我这个错误消息:消息:屏幕截图:可通过Screen获得你能给出整个错误消息吗??另外,您运行的脚本似乎正在运行其他一些函数Execute_脚本不起作用:仍然有此无法找到的元素。。。烦人的信息。我更新了我的第一篇文章并粘贴了html@Katie Hi,根据提供的HTML代码,对于超过18个按钮,类为btn btn success,因此在此处使用cssselector As.btn.btn-success我希望这是正确的查找方法?这可能是问题所在,您的解决方案向我显示:消息:无法连接到端口2156上的GhostDriver
driver.switch_to.frame("frame_name_or_id")
driver.find_element_by_class_name("enter_pl").click()
def get_browser():
    return webdriver.PhantomJS('phantomjs.exe', service_args=['--ignore-ssl-errors=true', '–ssl-protocol=any'])