selenium跳转网页无法定位元素 no such element: Unable to locate element

问题描述:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

selenium直接进入csdn网址执行登陆,可以完成。

可是

selenium先进入百度在搜索并进入csdn却提示定位不到页面元素。定位代码没问题。

解决方案:

每个页面都有一个句柄,从百度跳转到csdn是句柄信息没有改变,加一句

num=driver.window_handles

print(num)

driver.switch_to_window(num[1])

即可。

代码如下,小白勿喷。

 

from selenium import webdriver

from time import sleep

driver=webdriver.Chrome()

driver.get("https://www.baidu.com")    #先进百度

sleep(2)

driver.find_element_by_id("kw").send_keys("csdn")    #输入csdn

sleep(2)

driver.find_element_by_id("su").click()    #点击搜索

sleep(2)

driver.find_element_by_class_name("favurl").click()    #选择第一条查询结果

sleep(1)

num=driver.window_handles    #获取所有页面句柄

print(num)

driver.switch_to_window(num[1])    #跳转到csdn页面

driver.find_element_by_css_selector("li.userinfo>a").click()    #登陆

driver.find_element_by_css_selector("div.login-part>h3>a").click()    #账号密码登陆

sleep(3)

driver.find_element_by_css_selector("input#username").send_keys("123456")    #账号

sleep(3)

driver.find_element_by_css_selector("input#password").send_keys("123456")    #密码

sleep(1)

driver.find_element_by_class_name("logging").click()    #点击登陆

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
I'm sorry to hear that you're encountering a `NoSuchElementException` while using Selenium. This exception occurs when the element you're trying to locate or interact with cannot be found on the web page. To troubleshoot this issue, I suggest the following steps: 1. Verify the element locator: Double-check the correctness of the element locator (e.g., ID, class name, XPath, etc.) you are using to locate the element. Ensure that it accurately represents the element you're trying to interact with. 2. Wait for the element to appear: Sometimes, the element may take some time to load on the page. Implement an appropriate wait strategy like `WebDriverWait` to wait for the element to become visible or clickable before interacting with it. 3. Check if the element is present within an iframe: If the element you're trying to locate is within an iframe, you need to switch to that iframe context before locating the element. Use `driver.switch_to.frame()` method to switch to the correct iframe. 4. Ensure the element is not inside a shadow DOM: If the element is inside a shadow DOM, you won't be able to access it directly. You'll need to use `execute_script()` method to execute JavaScript code within the shadow DOM and perform your desired actions. 5. Verify if there are any dynamic elements: Some web pages dynamically load elements or change their structure after initial page load. If this is the case, ensure that you are locating the element after it has been fully loaded or after any dynamic changes have occurred. If none of these steps resolve the issue, providing more specific details about your code and the element you're trying to interact with would be helpful in further troubleshooting.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值