会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132471个问题
JAVA 全系列/第八阶段:SpringBoot与MybatisPlus/MybatisPlus 30736楼

半夜突然醒来,闲着无聊敲了个爬虫的代码,用selenium实现一个自动登录之类的,代码如下所示:

"""
   用 selenium 实现对中国大学mocc的登录
   程序运行报错:正在处理中
"""
from selenium import webdriver
from time import sleep
fox = webdriver.Firefox()
url ='https://www.icourse163.org/member/login.htm?returnUrl=aHR0cHM6Ly93d3cuaWNvdXJzZTE2My5vcmcvaW5kZXguaHRt#/webLoginIndex'
fox.get(url)
sleep(3)

# 点击登录按钮,弹出登录界面
fox.find_element_by_css_selector('#auto-id-1628452551743').click()
sleep(1)
# 选择其他登录方式
fox.find_element_by_css_selector('#login-cnt > div > div > div > div.ux-login-set-scan-code_ft > span').click()
# 获取账号框和密码框,输入密码
fox.find_element_by_css_selector('#auto-id-1628452775609').send_keys('*************')
sleep(1)
fox.find_element_by_css_selector('#auto-id-1628452775612').send_keys('*************')
sleep(1)
fox.find_element_by_css_selector('#dologin').click()
sleep(1)

print(fox.current_url)
print(fox.page_source)
sleep(5)


sleep(2)
fox.quit()

报错信息如下:

     

C:\Users\Administrator\AppData\Local\Programs\Python\Python39\python.exe D:/pythonProject2/实战python网络爬虫/selenium的使用/selenium_03.py

Traceback (most recent call last):

  File "D:\pythonProject2\实战python网络爬虫\selenium的使用\selenium_03.py", line 13, in <module>

    fox.find_element_by_css_selector('#auto-id-1628452551743').click()

  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 598, in find_element_by_css_selector

    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)

  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element

    return self.execute(Command.FIND_ELEMENT, {

  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute

    self.error_handler.check_response(response)

  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: #auto-id-1628452551743


无法定位到元素,敢问老师怎么处理?

进程已结束,退出代码为 1


Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 30737楼
JAVA 全系列/预科阶段:职业规划/学习方法/JAVA 技术体系介绍和学习方法 30739楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 30740楼
Python 全系列/第一阶段:Python入门/编程基本概念 30742楼
Python 全系列/第一阶段:Python入门/控制语句 30744楼
JAVA 全系列/预科阶段:职业规划/学习方法/程序员的基本素养和职业规划 30747楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 30748楼

打印出来为什么是字母隔断两个呢?逻辑有问题?

Printer:

package cn.sxt.thread;

public class Printer {
	private  int index=1;
	public  synchronized void print(int number) throws InterruptedException{
		if(index%3==0){
			super.wait();			
		}else{			
			System.out.println(number);
			super.notifyAll();
			index++;
	}
		
	}
	public  synchronized void print(char ch) throws InterruptedException{
		if(index%3!=0){
			super.wait();
		}else{
			System.out.println(ch);
			super.notifyAll();
			index++;
		}
	}

}

CharPrinter:

package cn.sxt.thread;

public class CharPrinter implements Runnable {
	private Printer printer;
	public CharPrinter(Printer printer){
		this.printer=printer;
	}
	@Override
	public void run() {
		for(char i='A';i<='Z';i++){
			try {
				printer.print(i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	

}

NumberPrinter :

package cn.sxt.thread;

public class NumberPrinter implements Runnable {
	private Printer printer;
	public NumberPrinter(Printer printer){
		this.printer=printer;
	}

	@Override
	public void run() {
		for(int i=1;i<=52;i++){
			try {
				printer.print(i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

}

TestThread

package cn.sxt.thread;

public class TestThread {
	public static void main(String[] args){
		Printer p = new Printer();
		NumberPrinter np = new NumberPrinter(p);
		CharPrinter cp = new CharPrinter(p);
		Thread t1 = new Thread(np);
		Thread t2 = new Thread(cp);
		t1.start();
		t2.start();
	}

}

image.png

MyNewThread.rar


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 30749楼
Python 全系列/第一阶段:Python入门/编程基本概念 30750楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637