【问题标题】:Selenium Timeoutexception Error [duplicate]Selenium 超时异常错误 [重复]
【发布时间】:2018-06-17 07:51:34
【问题描述】:

仍在解决这个 Instagram 问题,我真的需要你的帮助。

这是我的代码:

input_button = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH, 
'//button[@class ="chBAG"]')))

action=ActionChains(browser)
action.move_to_element(input_button)
action.click()
action.perform()

这是 HTML:

<button class="chBAG">Fermer</button>

但我得到了一个:

selenium.common.exceptions.TimeoutException: Message: 

有人可以帮我解决这个问题吗?

谢谢

【问题讨论】:

  • 这里不是一回事,它有一个按钮,顺便说一句,没有人帮我解决搜索栏问题,所以在说这是重复之前,你能帮我解决它吗?这样我可以编辑另一个一个
  • 您没有发布相关的html,只有一行异常。我们能帮你的不多。我在猜测 &lt;frame&gt; 中的元素,但这只是猜测。
  • 我用带有html的帖子编辑它,所以你可以帮助我;)
  • 这里是链接:instagram.com/accounts/login 我的目标是连接帐户(完成并且可以工作)然后关闭弹出窗口,然后在研究栏中搜索内容

标签: python selenium


【解决方案1】:

根据您的要求,您可以使用此代码:

它会在 Instagram 的搜索栏中搜索“This is testing”字符串。

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

driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.get("https://www.instagram.com/accounts/login/")

username = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "username")))

password = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "password")))

username.send_keys("your username")
password.send_keys("your password")
driver.find_element_by_tag_name('button').click()

search = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//span[text()='Search']")))

search.click()
driver.find_element_by_xpath("//div[@role='dialog']/preceding-sibling::input").send_keys("This is testing")

【讨论】:

  • 你能提供最新情况吗?
【解决方案2】:

由于输入字段的通用类,您会收到此错误。每次打开页面时,类名都会随机生成。那么如何解决呢?比如这样:

假设你想登录,你必须:

  1. 点击email输入栏
  2. 类型信息
  3. 点击password输入栏
  4. 类型信息
  5. 点击Log in按钮

示例代码可能是这样的:

# xpath will work every time because it is static
email_input = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH, 
'//form/div[1]/div/div/input'))) # locate email input
email_input =ActionChains(browser)
email_input.move_to_element(email_input)
email_input.click()
email_input.sendKeys("email")
email_input.perform()

password_input = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH, 
'//form/div[2]/div/div/input'))) # locate password input
password_input =ActionChains(browser)
password_input.move_to_element(password_input)
password_input.click()
email_input.sendKeys("password")
password_input.perform()

login_button = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH, 
'//span/button'))) # locate login button
login_button_action=ActionChains(browser)
login_button_action.move_to_element(login_button )
login_button_action.click()
login_button_action.perform()

要在搜索栏中搜索内容,您必须这样做:

  1. 点击search输入栏
  2. 类型信息
  3. 等到results 加载
  4. 点击results之一

代码:

import time # will be need below

search_input = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH, 
"//input[@placeholder = 'Search']"))) # locate search input
search_input =ActionChains(browser)
search_input.move_to_element(search_input)
search_input.click()
search_input.sendKeys("fermer")
search_input.perform()

time.sleep(5) # wait 5 seconds until dropdown will appear

dropdown_menu = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH, 
"//a[@href = '/explore/tags/fermermaid/']"))) # locate fermeraid
dropdown_menu = ActionChains(browser)
dropdown_menu.move_to_element(dropdown_menu)
dropdown_menu.click()
dropdown_menu.perform()

【讨论】:

    猜你喜欢
    • 2020-12-09
    • 2021-03-31
    • 1970-01-01
    • 2021-02-25
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多