python - Selenium 超时异常错误
问题描述
仍在解决这个 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:
有人可以帮我解决这个问题吗?
谢谢
解决方案
由于输入字段的通用类,您会收到此错误。每次打开页面时,类名都会随机生成。那么如何解决呢?例如像这样:
假设您想登录,您必须:
- 点击
email
输入字段 - 类型信息
- 点击
password
输入字段 - 类型信息
- 点击
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()
要在搜索栏中搜索某些内容,您必须执行以下操作:
- 点击
search
输入字段 - 类型信息
- 等到
results
会加载 - 单击其中一个
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()
推荐阅读
- python - 在 Python 中创建具有两个 y 轴和相同 x 轴/数据的图形
- php - Docker php56-fpm - 调用未定义的函数 apache_note()
- r - rbind 与部分列匹配?
- react-native - React-Native-maps 如何在 MapView 中不断更新区域?
- visual-studio-code - 如何为远程添加更多支持的文件扩展名(例如 .cnf) - SSH:编辑配置文件
- ruby - 尝试在 mac 上安装 sqlite3“sudo gem install sqlite3”时出错
- python - 防止 Pytest 在不同的测试文件之间重用 app 对象
- javascript - 强制数字 JSON 字段以避免科学记数法
- json - Flink - 导入类型不能修复“找不到类型的证据参数的隐式值....TypeInformation”
- speech-recognition - 使用阿里云进行实时翻译