简体   繁体   English

如何获取CSRF令牌并使用Java将其传递到Selenium WebDriver中以自动执行登录功能以防止自动化脚本进入网站

[英]How to get CSRF Token and pass it into Selenium WebDriver with Java to automate login function for website preventing automation script

I am a novice in Selenium with Java. 我是Selenium with Java的新手。 I have a tricky case that needs support from an expert. 我有一个棘手的案例,需要专家的支持。 I am trying to automate an e-commerce site with Selenium WebDriver JAVA. 我正在尝试使用Selenium WebDriver JAVA自动化电子商务站点。 As a manual, I login successfully with my registered credential. 作为手册,我使用注册凭证成功登录。 But when I run my automation script for this login function, the site prevents me from automating this by not generating the CSRF Token for my logging in a request. 但是,当我为该登录功能运行自动化脚本时,该站点通过不为我的登录请求生成CSRF令牌来阻止我将其自动化。 Every login script running with a validly registered credential, the "Invalid CSRF Token" message is displayed. 使用有效注册的凭据运行的每个登录脚本都会显示“ Invalid CSRF Token”消息。 I know that this site is preventing automation script for spam blocking. 我知道该站点正在阻止用于阻止垃圾邮件的自动化脚本。 But this is my exercise test and I must pass this. 但这是我的运动测试,我必须通过。 So I post this for the support request that whether I bypass the CSRF Token in the login script for automating this smoothly? 因此,我将其发布为支持请求,即是否绕过登录脚本中的CSRF令牌以使其顺利实现自动化?

Thank you for spending your time and reading my case. 感谢您花时间阅读我的案子。

You need to get the CSRF token value and set in request header to resolve this issue. 您需要获取CSRF令牌值并在请求标头中进行设置才能解决此问题。

Please find Robot framework snap of the same issue, 请找到相同问题的Robot框架快照,

${status} ${cookie_CSRF}= Run Keyword And Ignore Error Selenium2Library.Get Cookie Value X-CSRF Run Keyword If '${status}'=='PASS' Set Request Header X-CSRF ${cookie_CSRF} $ {status} $ {cookie_CSRF} =运行关键字并忽略错误Selenium2Library。获取Cookie值X-CSRF运行关键字如果'$ {status}'=='PASS'设置请求标头X-CSRF $ {cookie_CSRF}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM