avatarJeromy-Yu von 寰雨膠事錄

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2629

Abstract

le;"</span>) <span class="hljs-comment"># Show the result.</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"The webpage's title is:"</span>, title) <span class="hljs-comment"># Close the driver.</span> driver.quit()</pre></div><p id="7c03">This script runs JavaScript to get the current webpage’s title, which is then stored in the Python variable <code>title</code> and printed. You can replace "return document.title;" with any JavaScript that returns data, which will then be passed back to your Python script.</p><p id="feca">Passing Data to JavaScript Arguments can be passed to the execute_script() function in Selenium with Python. These arguments are then available in your JavaScript code, accessed through arguments[index], with ‘index’ denoting the position of the argument.</p><div id="f021"><pre><span class="hljs-keyword">from</span> selenium <span class="hljs-keyword">import</span> webdriver <span class="hljs-comment"># Initialize the WebDriver, using ChromeDriver here.</span> driver = webdriver.Chrome(<span class="hljs-string">'/path/to/chromedriver'</span>) <span class="hljs-comment"># Go to a website.</span> driver.get(<span class="hljs-string">'http://example.com'</span>) <span class="hljs-comment"># Define a JavaScript script with parameters.</span> js_code = <span class="hljs-string">"return arguments[0] + arguments[1];"</span> <span class="hljs-comment"># Pass arguments to execute_script</span> result = driver.execute_script(js_code, <span class="hljs-number">10</span>, <span class="hljs-number">20</span>) <span class="hljs-comment"># Show the result.</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"The calculated result is:"</span>, result) <span class="hljs-comment"># End the WebDriver session.</span> driver.quit()</pre></div><p id="81b9">In this sample, the JavaScript code is programmed to sum two numbers, with 10 and 20 being passed as arguments. The result is then stored in <code>result</code> and displayed.</p><p id="3cb2">Direct insertion of variables into the JavaScript string is another method, though it requires careful handling of quotes and strings.</p><p id="dc38">Managing Complex JavaScript Objects The execute_script() function can also manage complex JavaScript objects. This is particularly useful for interacting with objects within the structure of a webpage, like in single-page applications (SPAs).</p><div id="7f18"><pre><span class="hljs-comment"># Example: Accessing a complex JavaScript object.</span> complex_object = driver.execute_script(<span class="hljs-string">"return window.someComplexObject;"</spa

Options

n>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Retrieved Complex Object:"</span>, complex_object)</pre></div><p id="0188">Working with Browser Storage execute_script() can also be used for interactions with local storage and session storage in a browser, which is beneficial for setting or retrieving items for testing.</p><div id="ef3f"><pre><span class="hljs-comment"># Add an item to local storage.</span> driver.execute_script(<span class="hljs-string">"localStorage.setItem('key', 'value');"</span>) <span class="hljs-comment"># Retrieve an item from local storage.</span> value = driver.execute_script(<span class="hljs-string">"return localStorage.getItem('key');"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Data from Local Storage:"</span>, value)</pre></div><p id="80f4">Running Asynchronous JavaScript The execute_script() method in Selenium can execute asynchronous JavaScript, crucial for handling AJAX calls or delayed operations.</p><div id="8c1f"><pre><span class="hljs-comment"># Example: Running asynchronous JavaScript</span> async_result = driver.execute_script(<span class="hljs-string">"return new Promise(resolve => setTimeout(() => resolve('Result after 2 seconds'), 2000));"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Asynchronous output:"</span>, async_result)</pre></div><p id="c824">By understanding and utilizing the execute_script() method in Selenium with Python, a wide range of web automation and testing tasks become feasible, surpassing the limitations of standard Selenium APIs. Mastering this function ensures more thorough and effective automated testing scenarios.</p><p id="34f7">I hope this note will help someone to be more productive at work. If it’s useful, give it a like and subscribe to my <a href="https://medium.com/@itwebmind.com/about">Medium</a> so you don’t miss out 😊</p><p id="8a5e">More useful articles on my website <a href="http://itwebmind.com/">ItWebMind.com</a>, <a href="https://www.facebook.com/itwebmind/">Facebook</a>, <a href="https://twitter.com/itwebmind">Twitter</a>, <a href="https://www.linkedin.com/company/itwebmind/">LinkedIn</a> and <a href="https://www.reddit.com/user/ITWEBMIND">Reddit</a>. You can support me on <a href="https://www.patreon.com/itwebmind">Patreon</a> or <a href="https://www.buymeacoffee.com/itwebmind">ByMeACoffee</a>. I published the original of this article here — «<a href="https://itwebmind.com/how-to-run-javascript-in-selenium-python">How to run JavaScript in Selenium Python, pass and get values from it</a>».</p></article></body>

露西亞記者「死而復生」到底 jm9

週三一度傳出,俄羅斯最著名的戰地記者,近年批評露西亞總統布丁很厲害的 Arkady Babchenko 畢申科,突然間被槍殺,我們自己也分享了一些新聞。

畢竟這不是第一次有俄羅斯流亡人士在基輔被殺,例如前政客 Denis Voronenkov,因此都不疑有詐,就照分享。

誰知到畢申科這筆真是有詐,就在週三晚傳出畢申科和烏克蘭國家安全局一起開記者會,生勾勾的出現。

最後大家都知道是假新聞,烏克蘭情報部門 (也是 FSB) 承認是他們製造死訊,為了引出有關的嫌犯。

傳媒俄羅斯方面要殺畢申科已經一段時間,甚至 FSB 計劃行動兩個月。

然後最初的新聞是畢申科被槍殺,槍手是對他住家開槍,送往醫院路上死亡。

在這個假新聞發布期間, FSB 已經抓到一個烏克蘭人,然後還在通緝另一位。

當然俄羅斯繼續一貫否認的態度,表示這是烏克蘭的自導自演的抹黑之類。

而畢申科則感謝烏克蘭FSB保護,以及對假新聞對人造成困擾而道歉。

路透社 / Sky / 自由歐洲電台

Russia
中文
Spy
Military
Journalism
Recommended from ReadMedium