selenium第一个小程序遇到的问题

解决Selenium与Firefox版本不兼容及火狐路径问题
当火狐浏览器未安装在默认路径时,使用Selenium需通过System.setProperty设置路径。此外,Selenium与Firefox版本不兼容会导致无法连接。解决方案包括更新Selenium版本或调整Firefox版本以匹配。例如,Firefox 32对应Selenium 2.43。
package selenium_autoTest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestHelloWorld {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          //代码如下,主要是打开百度,然后在搜索框输入glen,点击搜索按钮,关闭浏览器。
        //如果火狐浏览器没有默认安装在C盘,需要制定其路径
        System.setProperty("webdriver.firefox.bin", "E:/Program Files/Mozilla Firefox/firefox.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.baidu.com/");
        
        driver.manage().window().maximize();
         
         WebElement txtbox = driver.findElement(By.name("wd"));
         txtbox.sendKeys("selenium");
        
         WebElement btn = driver.findElement(By.id("su"));
         btn.click();
         
        driver.close();

    }
    

}

如果火狐浏览器没有按照默认安装到C盘,需要制定其路径

  System.setProperty("webdriver.firefox.bin", "E:/Program Files/Mozilla Firefox/firefox.exe"); 

本人是装在.bin", "E:/Program Files/Mozilla Firefox/firefox.exe下面

否则会报异常

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: XP
Build info: version: '2.18.0', revision: '15704', time: '2012-01-27 17:37:17'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_23'

解决方法:方法1、最简单的重新安装firefox到默认路径。哈哈

             方法2、直接用System.setPropert方法设置webdriver.firefox.bin的值,如
Java代码

    System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");  

             方法3、 用FirefoxBinary类和public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)这个构造方法,直接上代码
Java代码

    File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
    FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);  
    WebDriver driver = new FirefoxDriver(firefoxbin,null);//这里使用这个构造方法。  

  应该还可以在环境变量里面设置firefox的路径也可以,有兴趣的可以试一下。



2  如果selenium和Firefox的版本不兼容,运行代码时,会出现下面异常:

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
tem-share
1413076587749    addons.xpi    INFO    Mapping {972ce4c6-7e08-4474-a285-3208198ce6fd} to E:\Program Files\Mozilla Firefox\browser\extensions\{972ce4c6-7e08-4474-a285-3208198ce6fd}
1413076587750    addons.xpi    INFO    Mapping fxdriver@googlecode.com to C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions\fxdriver@googlecode.com
1413076587750    addons.xpi    DEBUG    Ignoring file entry whose name is not a valid add-on ID: C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions\webdriver-staging
1413076587750    addons.xpi    DEBUG    checkForChanges
1413076587757    addons.xpi    DEBUG    Directory state JSON differs: cache [] state [{"name":"app-global","addons":{"{972ce4c6-7e08-4474-a285-3208198ce6fd}":{"descriptor":"E:\\Program Files\\Mozilla Firefox\\browser\\extensions\\{972ce4c6-7e08-4474-a285-3208198ce6fd}","mtime":1412224254982,"rdfTime":1411526203000}}},{"name":"app-profile","addons":{"fxdriver@googlecode.com":{"descriptor":"C:\\Users\\zhangmin\\AppData\\Local\\Temp\\anonymous3316082759202275717webdriver-profile\\extensions\\fxdriver@googlecode.com","mtime":1413076587152,"rdfTime":1413076586824}}}]
1413076587763    addons.xpi-utils    DEBUG    Opening XPI database C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions.json
1413076587766    addons.xpi    DEBUG    New add-on fxdriver@googlecode.com installed in app-profile
*** Blocklist::_loadBlocklistFromFile: blocklist is disabled
1413076588018    addons.xpi-utils    DEBUG    Make addon app-profile:fxdriver@googlecode.com visible
1413076588020    DeferredSave.extensions.json    DEBUG    Save changes
1413076588021    DeferredSave.extensions.json    DEBUG    Save changes
1413076588022    addons.xpi    DEBUG    New add-on {972ce4c6-7e08-4474-a285-3208198ce6fd} installed in app-global
1413076588025    addons.xpi-utils    DEBUG    Make addon app-global:{972ce4c6-7e08-4474-a285-3208198ce6fd} visible
1413076588025    DeferredSave.extensions.json    DEBUG    Save changes
1413076588025    DeferredSave.extensions.json    DEBUG    Save changes
1413076588033    addons.xpi    DEBUG    Updating database with changes to installed add-ons
1413076588033    addons.xpi-utils    DEBUG    Updating add-on states
1413076588034    addons.xpi-utils    DEBUG    Writing add-ons list
1413076589104    DeferredSave.extensions.json    DEBUG    Starting timer
1413076589138    addons.manager    DEBUG    shutdown
1413076589194    DeferredSave.extensions.json    DEBUG    Starting write
1413076589195    addons.xpi    DEBUG    shutdown
1413076589195    addons.xpi-utils    DEBUG    shutdown
1413076589263    DeferredSave.extensions.json    DEBUG    Write succeeded
1413076589263    addons.xpi-utils    DEBUG    XPI Database saved, setting schema version preference to 16
1413076589264    addons.xpi    DEBUG    Notifying XPI shutdown observers
1413076589266    addons.manager    DEBUG    Async provider shutdown done
1413076590479    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/addons/XPIProvider.jsm: ["XPIProvider"]
1413076590481    addons.manager    DEBUG    Loaded provider scope for resource://gre/modules/LightweightThemeManager.jsm: ["LightweightThemeManager"]
1413076590483    addons.xpi    DEBUG    startup
1413076590483    addons.xpi    DEBUG    Skipping unavailable install location app-system-local
1413076590483    addons.xpi    DEBUG    Skipping unavailable install location app-system-share
1413076590484    addons.xpi    INFO    Mapping {972ce4c6-7e08-4474-a285-3208198ce6fd} to E:\Program Files\Mozilla Firefox\browser\extensions\{972ce4c6-7e08-4474-a285-3208198ce6fd}
1413076590485    addons.xpi    INFO    Mapping fxdriver@googlecode.com to C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions\fxdriver@googlecode.com
1413076590485    addons.xpi    DEBUG    Ignoring file entry whose name is not a valid add-on ID: C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions\webdriver-staging
1413076590485    addons.xpi    DEBUG    checkForChanges
1413076590493    addons.xpi    DEBUG    No changes found
*** Blocklist::_preloadBlocklistFile: blocklist is disabled
JavaScript error: chrome://browser/content/urlbarBindings.xml, line 677: aUrl is undefined

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:106)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:244)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:110)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:190)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:183)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:179)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:92)
    at autoTest_selenium.TestHelloWorld.main(TestHelloWorld.java:13)



解决方案:

selenium版本过低,可以升级selenium,

window8装的Firefox是32版本,对应的selenium版本是2.43


其他:

Selenium  ->FireFox

2.25.0   ->  18

2.30.0   ->19

2.31.0   ->20

升级selenium jar包

正常启动firefox



### 抢票小程序的实现方法 比赛抢票小程序通常基于自动化脚本或浏览器插件实现,主要目的是在售票平台开放购票通道后迅速完成选座、下单和支付流程。这种技术手段广泛应用于演唱会、体育赛事等热门活动的票务抢购场景中。 #### 技术原理与实现方式 1. **浏览器自动化工具** 使用如 Selenium、Playwright 或 Puppeteer 等工具可以模拟用户操作行为,包括打开目标网站、登录账号、选择场次座位、点击“立即购买”按钮等流程。这些工具支持多种编程语言,其中 Python 和 JavaScript 是最常用的选择。 ```python from selenium import webdriver from selenium.webdriver.common.by import By import time # 初始化浏览器驱动 driver = webdriver.Chrome() driver.get("https://www.damai.cn") # 登录大麦网账户 driver.find_element(By.ID, "login").click() time.sleep(5) # 跳转至目标演出页面 target_url = "https://detail.damai.cn/item.htm?itemId=1234567890" driver.get(target_url) # 循环检测是否可点击“立即购买” while True: try: buy_button = driver.find_element(By.CLASS_NAME, "buybtn") if buy_button.text == "立即购买": buy_button.click() break else: time.sleep(1) driver.refresh() except Exception as e: print(f"抢票失败:{e}") break ``` 2. **定时请求机制** 在抢票开始前,系统会不断刷新页面以获取最新的状态变化信息。这一过程可以通过设置定时器实现,例如每秒检查一次按钮状态或页面内容是否发生变化。一旦检测到购票按钮变为可点击状态,则立即执行后续操作[^2]。 3. **验证码识别与处理** 多数票务平台会在高并发抢票时引入图形验证码或滑块验证来防止机器人行为。因此,在实现过程中需要集成 OCR 识别服务(如云打码平台)或调用第三方 API 来自动识别验证码内容,从而绕过验证环节。 4. **多线程与并发控制** 为了提升抢票成功率,可以在一个程序中启动多个浏览器实例,并行访问不同场次或座位区域的页面。通过线程池管理机制,确保资源合理分配并避免系统负载过高。 5. **异常处理与日志记录** 实际运行过程中可能会遇到网络延迟、页面加载失败、元素定位错误等问题。为此,需在代码中加入重试机制和异常捕获逻辑,同时记录详细的操作日志以便调试分析。 #### 注意事项 - **合规性问题** 自动化抢票行为可能违反平台的服务条款,部分情况下会被封禁账号甚至触发法律风险。因此,在使用此类脚本前应充分了解相关政策规定。 - **性能优化建议** 使用无头浏览器模式(Headless)可以减少界面渲染开销;适当调整刷新频率以平衡响应速度与服务器压力;优先选择高性能计算设备部署脚本。 - **安全防护措施** 避免将账号密码硬编码在脚本中,建议采用加密存储或环境变量传递的方式。此外,定期更新依赖库版本以修复潜在的安全漏洞。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值