cefsharp系列四:模拟点击

Minor error in trying to simulate MouseLeftDown-MouseLeftUp event in CefSharp browser control

Hello,

I am using the CefSharp webbrowser control. I will explain what I am trying to do:

  1. I load in the URL: "https://uniswapv3.flipsidecrypto.com"

  2. I find the control which is a SLIDER handle by classname "irs-handle from" successfully! (See red arrow in the image below)

  3. I now find the X and Y coordinate for this SLIDER successfully!

  4. Now I want to simulate a MouseLeftDown click on this X and Y coordinate, --- and DRAG this SLIDER to the right a bit by calling the MouseLeftUp event with a new X coordinate.

When executing the code. This SLIDER is actually clicked on because the slider changes value a bit. So this part is successful. But it seems that the MouseLeftUp event with the new X coordinate doesn't release the slider to the right.

This is the slider I am trying to move in the URL (The round green handle, - where the red arrow(1,773) in the image below in pointing to)

(As seen in the end of the post I have posted an image of the MouseLeftDown/MouseLeftUp events. Somehow the website will not post the question if I post that code as it is)

I wonder what I could be missing. It has to be a minor detail missing? This is how I try to drag the slider to the right: (+5 to +100)

 
  1. //Now click and drag slider to the right
  2. MouseLeftDown(int.Parse(coordx) + 5, int.Parse(coordy) + 5);
  3. MouseLeftUp(int.Parse(coordx) + 100, int.Parse(coordy) + 5);


 

Thank you!

 
  1. private void button1_Click(object sender, EventArgs e) { new Thread(moveSlideControl).Start(); }
  2. void moveSlideControl()
  3. {
  4. var scriptTask = browser.EvaluateScriptAsync(@"
  5. var play = document.getElementsByClassName('irs-handle from')[0]
  6. function findPos(obj)
  7. {
  8. var curleft = 0;
  9. var curtop = 0;
  10. if (obj.offsetParent)
  11. {
  12. do
  13. {
  14. curleft += obj.offsetLeft;
  15. curtop += obj.offsetTop;
  16. } while (obj = obj.offsetParent);
  17. return { X: curleft,Y: curtop};
  18. }
  19. }
  20. findPos(play)"
  21. ).ContinueWith(x =>
  22. {
  23. // 2. Continue with finding the coordinates and using MouseClick method
  24. // for pressing left mouse button down and releasing it at desired end position.
  25. var responseForMouseClick = x.Result;
  26. if (responseForMouseClick.Success && responseForMouseClick.Result != null)
  27. {
  28. var xy = responseForMouseClick.Result;
  29. var json = JsonConvert.SerializeObject(xy).ToString();
  30. var coordx = json.Substring(json.IndexOf(':') + 1, 3);
  31. var coordy = json.Substring(json.LastIndexOf(':') + 1, 3);
  32. //Now click and drag slider to the right
  33. MouseLeftDown(int.Parse(coordx) + 5, int.Parse(coordy) + 5);
  34. MouseLeftUp(int.Parse(coordx) + 100, int.Parse(coordy) + 5);
  35. }
  36. });






dotnet-csharp

image.png (19.7 KiB)

image.png (40.8 KiB)

 Comment  Save

1 Answer

正在上传…重新上传取消

Click to vote1 Vote"1

TimonYang-MSFT answered • Jul 16 2021 at 2:20 PM | TimonYang-MSFT edited • Jul 19 2021 at 9:29 AM BEST ANSWERACCEPTED ANSWER

When executing the code. This SLIDER is actually clicked on because the slider changes value a bit.

I'm afraid not.

If this slider is indeed clicked, then when our mouse moves left and right, the slider will move with it, even if we did not click the slider (because we have clicked through the code).

But the current code does not seem to have this behavior, I still need to click on this slider and then move left and right.

After some testing, I found the cause of the problem.

The Y value of the position we need to pass to SendMouseClickEvent is shown in image 1. It is the position of the slider relative to the control. As we slide the wheel, it will change.

But the Y value obtained by the current JS code is shown in image 2. The position of the slider relative to the top of this website is fixed (as long as we don’t change the size of the page, if we adjust the page very narrowly,

Then the control will move down because of the re-formatting of the page).

So this code does not actually click the slider, which leads to the current confusion.

To capture the slider accurately, I modified the code. After clicking the button, hover the cursor on the slider, and then you can see the slider move with the execution of SendMouseMoveEvent.

 
  1. public Form1()
  2. {
  3. InitializeComponent();
  4. }
  5. private ChromiumWebBrowser browser;//CefSharp brows
  6. private void Form1_Load(object sender, EventArgs e)
  7. {
  8. browser = new ChromiumWebBrowser("https://uniswapv3.flipsidecrypto.com")
  9. {
  10. Dock = DockStyle.Fill,
  11. };
  12. this.Controls.Add(browser);
  13. }
  14. private void button1_Click(object sender, EventArgs e) { new Thread(moveSlideControl).Start(); }
  15. void moveSlideControl()
  16. {
  17. //I did it deliberately, it cannot be submitted when S*leep() is included in the code, you should delete the *.
  18. Thread.S*leep(2000);
  19. Point ptCursor = new Point(0, 0);
  20. this.Invoke(new Action(() =>
  21. {
  22. ptCursor = Cursor.Position;
  23. ptCursor = PointToClient(ptCursor);
  24. }));
  25. MouseLeftUp(ptCursor.X, ptCursor.Y);
  26. }
  27. private void MouseLeftUp(int v1, int v2)
  28. {
  29. var host = browser.GetBrowser().GetHost();
  30. host.SendMouseClickEvent(v1, v2, MouseButtonType.Left, false, 1, CefEventFlags.None);
  31. for (int i = 0; i < 7; i++)
  32. {
  33. v1 = v1 + i * 2;
  34. host.SendMouseMoveEvent(v1, v2, false, CefEventFlags.LeftMouseButton);//Move the mouse
  35. Thread.S*leep(1000);
  36. }
  37. }

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

capture.png (222.2 KiB)

1.png (122.8 KiB)

 Comment ·  Hide 1

正在上传…重新上传取消 Andreasss-5315 · Jul 16 2021 at 8:20 PM

Yes, I understand. I noticed also later that the JS code didn't capture the correct X and Y coordinates. The "SendMouseMoveEvent" was a gamechanger here! That was also really one problem I had to actually move the cursor.

It seems that the X and Y coordinates that was captured with JS, - and if adding 85 to the X and 123 to the Y coordinate. It seems to capture the control exactly. (It might be a clunky workaround though but it seems to work by doing that.)

Then, pass those coordinates to the:

 
  1. int Xextra = 85; int Yextra = 123; //Somehow by adding this seems to capture the control exactly
  2. MouseLeftUp(int.Parse(coordx) + Xextra, int.Parse(coordy) + Yextra);

Thank you for your help!



 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CefSharp一个基于谷歌开源项目Chromium Embedded Framework(CEF)的.NET封装库,可用于在Windows平台上开发基于Chromium的应用程序。要进行模拟登录,可以按照以下步骤操作: 1. 首先,引入CefSharp库到项目中。可以通过NuGet包管理器添加CefSharp.WinForms或CefSharp.Wpf(根据项目类型选择)依赖项。 2. 创建一个WebBrowser控件(WinForms)或ChromiumWebBrowser控件(WPF),用于显示网页。 3. 在代码中使用CefSharp的API进行模拟登录。可以使用控件的Load()方法加载登录页面,然后使用ExecuteScriptAsync()方法执行JavaScript代码来填写表单和点击登录按钮。 4. 创建一个CefSharp.IRequestHandler的实现类,并重写它的OnBeforeBrowse()方法。在该方法中,可以检测到需要登录的页面,并在请求中添加用户名和密码等登录信息。 5. 调用控件的RequestHandler属性,将第4步创建的请求处理程序实例赋值给它,以便在加载页面时拦截请求,并在其中添加登录信息。 6. 最后,在登录成功后,可以使用控件的Load()方法加载需要模拟登录后访问的页面,然后使用GetSourceAsync()方法获取该页面的HTML源码,或者使用ExecuteScriptAsync()方法执行JavaScript代码操作页面。 值得注意的是,在使用CefSharp进行模拟登录时,可能会遇到一些挑战,如验证码识别、动态生成的表单等问题。这可能需要进一步的研究和处理。同时,为了确保程序的稳定性和安全性,还应注意安全性措施,如加密传输、用户信息保护等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值