Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
Selenium驱动程序可以';登录网站后找不到任何元素[Python]_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Selenium驱动程序可以';登录网站后找不到任何元素[Python]

Selenium驱动程序可以';登录网站后找不到任何元素[Python],python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,首先,如果之前发生过类似的话题,我很抱歉,但我找不到任何像我这样的问题 我想创建一个简单的脚本,它进入一个电子邮件网站,登录到我的帐户,并找到未读邮件的数量 这是登录的部分 from selenium import webdriver from time import sleep class sMailBot(): def __init__(self): self.driver = webdriver.Chrome() def login(self):

首先,如果之前发生过类似的话题,我很抱歉,但我找不到任何像我这样的问题

我想创建一个简单的脚本,它进入一个电子邮件网站,登录到我的帐户,并找到未读邮件的数量

这是登录的部分

from selenium import webdriver
from time import sleep

class sMailBot():
    def __init__(self):
        self.driver = webdriver.Chrome()

    def login(self):
        self.driver.get('website.com')

        sleep(2)

        btn_login = self.driver.find_element_by_xpath('//*[@id="username"]')
        btn_login.send_keys('my_username')

        btn_password = self.driver.find_element_by_xpath('//*[@id="password"]')
        btn_password.send_keys('my_password')

        btn_logintoaccount = self.driver.find_element_by_xpath('//*[@id="button"]')
        btn_logintoaccount.click()

        sleep(5)
它工作得非常好。在登录到我的邮件帐户后,像driver.title或driver.current\u url这样的评论会起作用

现在我想刮取这部分html代码:

<b>some_important_string_which_stores_the_amount_of_unread_mails</b>
但是,它不起作用。此外,我从这边找不到任何其他元素

我想强调的是,我等待页面加载的时间甚至超过了10秒

发生的错误

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="MS_act1"]/span/b"}
  (Session info: chrome=80.0.3987.87)
正如您所要求的,我添加了一些周围的HTML代码

<span style="float: right">
<b>some_important_string_which_stores_the_amount_of_unread_mails</b> 
</span>

一些重要的字符串存储未读邮件的数量

请不要使用睡眠,这不是硒的好选择

相反,请使用以下命令:

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

首先,我将避免使用睡眠。您可以尝试使用。这将暂停浏览器,直到满足给定条件

e、 g.如下:

WebDriverWait(self.driver, 60).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Login']")))

这将最多等待60秒,元素(带有文本登录的按钮)才会出现在页面中。

在登录到您的邮件帐户后,命令如
driver.title
driver.current\u url
起作用,但它们不是页面的一部分

相关的HTML将帮助我们构建一个规范的答案。但是,要提取所需的文本,您必须为\u element\u located()的
可见性引入WebDriverWait,并且您可以使用以下任一选项:

  • 使用
    CSS\u选择器
    get\u属性(“innerHTML”)

  • 使用
    XPATH
    和文本属性:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[starts-with(@id, 'MS_act')]//span/b"))).text)
    
  • 注意:您必须添加以下导入:

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

它可能位于
中。而且你似乎也在寻找
标记,而不是
。如果你从开发工具运行搜索,它是否有效?正如Guy所说,检查它是否在IFRAME中。你说你在等10秒,但在你的代码里你只等了5秒。5秒可能还不够。你应该加上等待30秒,看看这是否有帮助。另外,请围绕您要查找的元素发布更多相关的HTML。编辑您的问题并添加失败时抛出的错误消息。谢谢。@Marco不,不行。@JeffC我提供了你要的东西。我试着用了你的提示。不幸的是,它没有起作用。我收到**TimeoutException:Message:*@Hendrra签出更新的答案并告诉我状态。@Hendrra签出更新的答案并告诉我状态。我重试。结果是一样的——TimeoutException:Message:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[starts-with(@id, 'MS_act')]//span/b"))).text)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC