如何在react-native中打开网址跳转到对应网址而不是在原页面?

本文介绍了如何在React Native的sectionScreen.js组件中打开http://www.baidu.com,而不是在原页面基础上打开。通过引入`react-native-webview`,创建WebView组件加载网页,并利用Linking的openURL()方法处理链接。当遇到返回时依然在原网页打开的问题,通过onShouldStartLoadWithRequest方法拦截URL加载,返回false阻止默认行为,从而解决了问题。

需求:在sectionScreen.js页面中打开http://www.baidu.com而不是在原页面的基础上打开

1.npm i  react-native-webview --save

2.WebView 创建一个原生的 WebView,可以用于访问一个网页,还可以直接嵌入 html 代码

详情:https://reactnative.cn/docs/next/webview

 <WebView
     source={
  
  {html:htmlContent+htmlStyles}}
     scalesPageToFit={false}
     scrollEnabled={false}
     onNavigationStateChange={ event =>{
           console.log(event)
                          
     if(event.url!='about:blank'){
      //Linking作用就是没有在页面中打开网址,直接去浏览器中打开
       Linking.openURL(event.url)
                 } 
           }}
      />

当导航状态发生变化时开始监听事件,可以看到event对象的内容

我们引入Linking提供了一个通用的接口来与传入和传出的 App 链接进行交互。

o

C:\own\app\python_code\.venv\Scripts\python.exe C:\own\app\python_code\work\IPL_click_all\full_automation.py 🚀 启动 Android 应用探索器... 🔧 正在处理初始化流程... 🔍 检测是否需要接受用户协议... 📄 检测到【用户协议】页面 👇 协议未读完,开始滚动... ✅ 第 3 次滑动后发现可点击的【I agree】按钮 📌 正在等待页面跳转... ❌ 第 3 次滑动后点击【I agree】无效,仍在页面 ✅ 第 4 次滑动后发现可点击的【I agree】按钮 📌 正在等待页面跳转... ❌ 第 4 次滑动后点击【I agree】无效,仍在页面 ✅ 第 5 次滑动后发现可点击的【I agree】按钮 📌 正在等待页面跳转... ❌ 第 5 次滑动后点击【I agree】无效,仍在页面 ✅ 第 7 次滑动后发现可点击的【I agree】按钮 📌 正在等待页面跳转... ✅ 成功跳转出协议页面 ✅ 用户协议处理完成 🖨️ 检测是否进入【打印机选择】页面... ✅ 进入打印机选择页面 👋 用户中断执行。 🔚 自动化结束。 Process finished with exit code -1 def handle_user_agreement(self): print("🔍 检测是否需要接受用户协议...") try: initial_activity = self.driver.current_activity except: initial_activity = None try: # 等待协议页面出现 WebDriverWait(self.driver, 10).until( EC.presence_of_element_located(( 'xpath', '//*[contains(@text, "License") or contains(@text, "Agreement") or contains(@text, "user")]' )) ) print("📄 检测到【用户协议】页面") def find_agree_button(): candidates = [ '//*[@text="I agree"]', '//*[@text="Agree"]', '//*[contains(@resource-id, "yes")]', '//*[contains(@resource-id, "agree")]', ] for xpath in candidates: try: btn = self.driver.find_element(By.XPATH, xpath) if btn.is_displayed() and btn.is_enabled() and self.is_fully_active(btn): return btn except: continue return None # 先尝试直接点击 agree_btn = find_agree_button() if agree_btn: print("✅ 发现可点击的【I agree】按钮") current_act_before = self.driver.current_activity agree_btn.click() time.sleep(3) if self.driver.current_activity != current_act_before: print("✅ 已跳转至新页面,协议处理完成") return True else: print("🟡 点击了【I agree】但未跳转,可能需滚动") # 开始滚动 print("👇 协议未读完,开始滚动...") last_click_failed_id = None for i in range(10): size = self.driver.get_window_size() x = size['width'] // 2 y_start = int(size['height'] * 0.8) y_end = int(size['height'] * 0.3) self.driver.swipe(x, y_start, x, y_end, 600) time.sleep(1.5) agree_btn = find_agree_button() if not agree_btn: continue # 防止重复点击同一个无效按钮 if hasattr(agree_btn, 'id') and agree_btn.id == last_click_failed_id: continue print(f"✅ 第 {i + 1} 次滑动后发现可点击的【I agree】按钮") current_act_before = self.driver.current_activity agree_btn.click() print("📌 正在等待页面跳转...") time.sleep(3) try: WebDriverWait(self.driver, 10).until( lambda d: d.current_activity != current_act_before ) print("✅ 成功跳转出协议页面") return True except: print(f"❌ 第 {i + 1} 次滑动后点击【I agree】无效,仍在页面") last_click_failed_id = agree_btn.id # 记录失败 ID # 兜底:UiAutomator 强制查找 clickable=true 的按钮 try: final_btn = self.driver.find_element( By.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("agree").enabled(true).clickable(true)' ) final_btn.click() print("🪄 使用 UiAutomator 强制点击【I agree】") time.sleep(3) if self.driver.current_activity != initial_activity: print("✅ 强制点击后成功跳转") return True except Exception as e: print(f"❌ UiAutomator 点击失败: {e}") return False except TimeoutException: print("🟢 未检测到用户协议页面,跳过") return True except Exception as e: print(f"⚠️ 用户协议处理异常: {type(e).__name__}: {e}") return False def is_fully_active(self, button): """判断按钮是否处于完全激活状态""" try: alpha = button.get_attribute("alpha") if alpha and float(alpha) < 0.8: return False except: pass try: class_name = button.get_attribute("class") if "disabled" in class_name.lower(): return False except: pass return True i agree变成可点击后会从灰色变为蓝色,增加is_full的判别
最新发布
10-18
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值