python - Selenium can't locate element after I change desktop
问题描述
So I have this part of the code:
buttons = browser.driver.find_element_by_id('buttons')
camera_icon = buttons.find_element_by_class_name('style-scope yt-icon-button')
camera_icon.click()
I am trying to click the upload button on youtube, it works perfectly fine when I stay on the browser's desktop(fullscreen). If I start the program and leave the webdriver to work in the background, while I navigate to another desktop, it can't locate 'style-scope yt-icon-button' for some reason.
Any help/tips on this is much appreciated!
解决方案
It might be timing issue. So make sure you are using appropriate wait mechanism in your script. Introduce implicit and explicit wait conditions.
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".style-scope.yt-icon-button"))
element.click()
Import below package for this
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Moreover, It seems the issue with your locator. there are 2 classes style-scope
and yt-icon-button
. as far as i know class selector find_element_by_class_name
doesn't support compound classes.
camera_icon = buttons.find_element_by_class_name('style-scope yt-icon-button')
should be
camera_icon = buttons.find_element_by_css_selector('.style-scope.yt-icon-button')
推荐阅读
- amazon-web-services - 如何保留客户端源 IP 以使用 AWS NLB 到 ALB Terraform Terragrunt 在中国设置白名单
- c# - 需要 D365/CRM 来识别以编程方式创建的 SharePoint 文件夹的 DocumentLocation 记录
- java - JavaFX 项目找不到我的 JDK FX 版本
- angular - Mat-Select 如何去掉小缩放
- azure-data-factory - JSON 数据显示为数组
- video-processing - Typ3 FAL Flexform 视频路径
- java - 在spring data mongo中,如何以一般方式从@Document对象生成更新集对象-或者如何upsert(@Document)?
- python - 如何检查某些代码是否释放了 GIL,所以多线程很有用?
- powershell - PowerShell Read-Host 将输入输出到控制台
- .net - 如何在 publishsinglefile .exe 上下文中查找“内容文件”(使用未知的 TEMP 文件夹)