繁体   English   中英

避免在python中输入javascript鸭子检测到Selenium?

[英]Avoid Selenium to be detected by javascript duck typing in python?

有什么方法可以更改 Selenium 属性以避免通过 Javascript 中的鸭子类型方法检测到它自己?

上次我几乎不想让 Selenium 完全不被检测到并且与 Chrome 相同。 几乎成功了,但刚刚发现了一种我无法绕过的新检测方法:鸭子在 Javascript 中输入。

这是Javascript检测代码:

// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';

// Safari 3.0+ "[object HTMLElementConstructor]" 
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;

// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;

// Chrome 1 - 71
var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);

// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;


var output = 'Detecting browsers by ducktyping:<hr>';
output += 'isFirefox: ' + isFirefox + '<br>';
output += 'isChrome: ' + isChrome + '<br>';
output += 'isSafari: ' + isSafari + '<br>';
output += 'isOpera: ' + isOpera + '<br>';
output += 'isIE: ' + isIE + '<br>';
output += 'isEdge: ' + isEdge + '<br>';
output += 'isBlink: ' + isBlink + '<br>';
document.body.innerHTML = output;

来源: https : //jsfiddle.net/6spj1059/看看这个js代码在真实Chrome浏览器中的结果是什么: results for real Chrome browser

虽然在 Selenium 中打开相同的jsfiddle链接时所有这些都是假的。

这是我对 Python 所做的。 我试图通过将其声明为 Chrome 来更改 python 中的窗口标题,因为直接设置它是不可能的。 还尝试先导航到 chrome://newtab 网址,然后导航到我想要的网址,但也失败了。

browser.get("chrome://newtab")
try:
    assert u'New Tab' in browser.title
    print "Assertion succeed!"
    print browser.title
except AssertionError:
    _, _, tb = sys.exc_info()
    traceback.print_tb(tb)  # Fixed format
    tb_info = traceback.extract_tb(tb)
    filename, line, func, text = tb_info[-1]

    print('An error occurred on line {} in statement {}'.format(line, text))
    exit(1)

print("Page Title is :", browser.title)
clear_cache_and_cookies(browser)

browser.get(url16)
print browser.title
print type(browser.title)
print u'New Tab'
print type(u'New Tab')

感谢您的帮助!

应该:

driver.execute_script("""
  window.chrome.webstore = true
""")

暂无
暂无

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

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