GVKun编程网logo

Selenium Webdriver单击Google搜索(selenium打开谷歌浏览器)

4

关于SeleniumWebdriver单击Google搜索和selenium打开谷歌浏览器的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Java如何强制SeleniumWebDriver

关于Selenium Webdriver单击Google搜索selenium打开谷歌浏览器的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Java 如何强制Selenium WebDriver单击当前不可见的元素?、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、ruby – 无法使用Selenium WebDriver单击Safari中的按钮、Selenium WebDriver 在导入 selenium 时不可调用错误但在不导入 selenium 时有效等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

Selenium Webdriver单击Google搜索(selenium打开谷歌浏览器)

Selenium Webdriver单击Google搜索(selenium打开谷歌浏览器)

我正在搜索文字“奶酪!”
在Google主页上,并不确定在按搜索按钮后如何才能单击搜索到的链接。例如,我想单击搜索页面顶部的第三个链接,然后如何找到标识的链接并单击它。到目前为止,我的代码:

package mypackage;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.WebDriverWait;public class myclass {    public static void main(String[] args) {        System.setProperty("webdriver.chrome.driver", "C:\\selenium-java-2.35.0\\chromedriver_win32_2.2\\chromedriver.exe");         WebDriver driver = new ChromeDriver();          driver.get("http://www.google.com");          WebElement element = driver.findElement(By.name("q"));         element.sendKeys("Cheese!");         element.submit();         //driver.close();    }}

答案1

小编典典

Google缩小了CSS类等,因此识别所有内容并不容易。

另外,您还有一个问题,必须“等待”,直到站点显示结果为止。我会这样做:

public static void main(String[] args) {    WebDriver driver = new FirefoxDriver();    driver.get("http://www.google.com");    WebElement element = driver.findElement(By.name("q"));    element.sendKeys("Cheese!\n"); // send also a "\n"    element.submit();    // wait until the google page shows the result    WebElement myDynamicElement = (new WebDriverWait(driver, 10))              .until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));    List<WebElement> findElements = driver.findElements(By.xpath("//*[@id=''rso'']//h3/a"));    // this are all the links you like to visit    for (WebElement webElement : findElements)    {        System.out.println(webElement.getAttribute("href"));    }}

这将为您打印:

  • http://de.wikipedia.org/wiki/奶酪
  • http://en.wikipedia.org/wiki/奶酪
  • http://www.dict.cc/englisch-deutsch/cheese.html
  • http://www.cheese.com/
  • http://projects.gnome.org/cheese/
  • http://wiki.ubuntuusers.de/奶酪
  • http://www.ilovecheese.com/
  • http://cheese.slowfood.it/
  • http://cheese.slowfood.it/en/
  • http://www.slowfood.de/termine/termine_international/cheese_2013/

Java 如何强制Selenium WebDriver单击当前不可见的元素?

Java 如何强制Selenium WebDriver单击当前不可见的元素?

我在FirefoxDriver中使用Selenium 2 Java API。当我填写表单时,根据表单输入,将复选框添加到页面。

我想使用Selenium模拟对这些复选框的单击。该元素在常规浏览器中可见且可用,但是selenium断言该元素不可见。

"Element is not currently visible and so may not be interacted with"

我可以强迫selenium忽略元素的不可见状态吗?如何强制Selenium与不可见元素进行交互?

答案1

小编典典

Selenium根据以下条件确定元素是否可见(使用DOM检查器确定哪些CSS适用于你的元素,请确保你查看计算的样式):

  • visibility != hidden
  • display != none (is also checked against every parent element)
  • opacity != 0 (this is not checked for clicking an element)
  • height and width are both > 0
  • for an input, the attribute type != hidden

你的元素符合这些条件之一。如果你没有能力更改元素的样式,请按照以下方法用javascript强制执行此操作(由于你使用了Selenium2 API,因此假设使用WebDriver):

((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", inputElement);

但这不会触发javascript事件,如果你依赖于该输入的change事件,那么你也必须触发它(有多种方法,最容易使用该页面上加载的任何javascript库)。

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

如何解决JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测?

从当前的实现开始,一种理想的访问网页而不被检测到的方法是使用ChromeOptions()该类向以下参数添加几个参数:

排除enable-automation开关的集合 关掉 useAutomationExtension 通过以下实例ChromeOptions

Java示例:

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\browserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver =  new ChromeDriver(options);
driver.get("https://www.google.com/");

Python范例

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option(''useAutomationExtension'', False)
driver = webdriver.Chrome(options=options, executable_path=r''C:\path\to\chromedriver.exe'')
driver.get("https://www.google.com/")

解决方法

我正在尝试使用selenium和铬在网站中自动化一个非常基本的任务,但是以某种方式网站会检测到铬是由selenium驱动的,并阻止每个请求。我怀疑该网站是否依赖像这样的公开DOM变量https://stackoverflow.com/a/41904453/648236来检测selenium驱动的浏览器。

我的问题是,有没有办法使navigator.webdriver标志为假?我愿意尝试修改后重新尝试编译selenium源,但是似乎无法在存储库中的任何地方找到NavigatorAutomationInformation源https://github.com/SeleniumHQ/selenium

任何帮助深表感谢

PS:我还从https://w3c.github.io/webdriver/#interface尝试了以下操作

Object.defineProperty(navigator,''webdriver'',{
    get: () => false,});

但是它仅在初始页面加载后更新属性。我认为网站会在执行脚本之前检测到变量。

ruby – 无法使用Selenium WebDriver单击Safari中的按钮

ruby – 无法使用Selenium WebDriver单击Safari中的按钮

我想点击下面的按钮但是当我没有点击运行脚本按钮时,脚本仍在传递.

这是HTML剪辑

<input type="button" value="Authorize"id="oauth2button">

这是我用来执行点击操作的ruby代码

authorize_button = $driver.find_element(:id,'oauth2button')
authorize_button.click

如果我使用authorize_button.displayed?它返回true.
我使用的是Selenium WebDriver,Safari版本是6.1.1

解决方法

这可能不是您正在寻找的答案,但是当我遇到这类问题时,我做过的一件事就是使用JavaScript来执行特定的操作而不是Selenium,这对我来说每次都有用.你可以尝试类似的东西:

$driver.execute_script("document.getElementById('oauth2button').click();")

Selenium WebDriver 在导入 selenium 时不可调用错误但在不导入 selenium 时有效

Selenium WebDriver 在导入 selenium 时不可调用错误但在不导入 selenium 时有效

如何解决Selenium WebDriver 在导入 selenium 时不可调用错误但在不导入 selenium 时有效?

我正在尝试抓取一些 LinkedIn 个人资料,但是,使用下面的代码,给了我一个错误:

错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-b6cfafdd5b52> in <module>
     25     #sending our driver as the driver to be used by srape_linkedin
     26     #you can also create driver options and pass it as an argument
---> 27     ps = ProfileScraper(cookie=myLI_AT_Key,scroll_increment=random.randint(10,50),scroll_pause=0.8 + random.uniform(0.8,1),driver=my_driver)  #changed name,default driver and scroll_pause time and scroll_increment made a little random
     28     print(''Currently scraping: '',link,''Time: '',datetime.Now())
     29     profile = ps.scrape(url=link)       #changed name

~\Anaconda3\lib\site-packages\scrape_linkedin\Scraper.py in __init__(self,cookie,scraperInstance,driver,driver_options,scroll_pause,scroll_increment,timeout)
     37 
     38         self.was_passed_instance = False
---> 39         self.driver = driver(**driver_options)
     40         self.scroll_pause = scroll_pause
     41         self.scroll_increment = scroll_increment

TypeError: ''WebDriver'' object is not callable

代码:

    from datetime import datetime
        from scrape_linkedin import ProfileScraper
        import random                       #new import made
        from selenium import webdriver  #new import made
        import pandas as pd
        import json
        import os
        import re
        import time
        
        os.chdir("C:\\Users\\MyUser\\DropBox\\linkedInScrapper\\")
        
my_profile_list = [''https://www.linkedin.com/in/williamhgates/'',''https://www.linkedin.com/in/christinelagarde/'',''https://www.linkedin.com/in/ursula-von-der-leyen/'']
        
        myLI_AT_Key = MyKey # you need to obtain one from Linkedin using these steps:
    
    # To get LI_AT key
    # Navigate to www.linkedin.com and log in
    # Open browser developer tools (Ctrl-Shift-I or right click -> inspect element)
    # Select the appropriate tab for your browser (Application on Chrome,Storage on Firefox)
    # Click the Cookies dropdown on the left-hand menu,and select the www.linkedin.com option
    # Find and copy the li_at value
        
        for link in my_profile_list:
        
            #my_driver = webdriver.Chrome()  #if you don''t have Chromedrive in the environment path then use the next line instead of this
            #my_driver = webdriver.Chrome()
            my_driver = webdriver.Firefox(executable_path=r''C:\Users\MyUser\DropBox\linkedInScrapper\geckodriver.exe'')
            #my_driver = webdriver.Chrome(executable_path=r''C:\Users\MyUser\DropBox\linkedInScrapper\chromedriver.exe'')
            #sending our driver as the driver to be used by srape_linkedin
            #you can also create driver options and pass it as an argument
            ps = ProfileScraper(cookie=myLI_AT_Key,default driver and scroll_pause time and scroll_increment made a little random
            print(''Currently scraping: '',datetime.Now())
            profile = ps.scrape(url=link)       #changed name
            dataJSON = profile.to_dict()
        
            profileName = re.sub(''https://www.linkedin.com/in/'','''',link)
            profileName = profileName.replace("?originalSubdomain=es","")
            profileName = profileName.replace("?originalSubdomain=pe","")
            profileName = profileName.replace("?locale=en_US","")
            profileName = profileName.replace("?locale=es_ES","")
            profileName = profileName.replace("?originalSubdomain=uk","")
            profileName = profileName.replace("/","")
        
            with open(os.path.join(os.getcwd(),''ScrapedLinkedInprofiles'',profileName + ''.json''),''w'') as json_file:
                json.dump(dataJSON,json_file)
                time.sleep(10 + random.randint(0,5))    #added randomness to the sleep time
            #this will close your browser at the end of every iteration
            my_driver.quit()
        
        
        
        print(''The first observation scraped was:'',my_profile_list[0:])
        print(''The last observation scraped was:'',my_profile_list[-1:])
        print(''END'')

我尝试了许多不同的方法来尝试让 webdriver.Chrome() 工作,但没有任何运气。我曾尝试使用 Chrome (chromedriver) 和 Firefox (geckodriver),尝试以多种不同的方式加载 selenium 包,但我一直收到错误 TypeError: ''WebDriver'' object is not callable

我下面的原始代码仍然有效。 (即它会打开 Google Chrome 浏览器并转到 my_profiles_list 中的每个配置文件,但我想使用上面的代码。

from datetime import datetime
from scrape_linkedin import ProfileScraper
import pandas as pd
import json
import os
import re
import time

my_profile_list = [''https://www.linkedin.com/in/williamhgates/'',''https://www.linkedin.com/in/ursula-von-der-leyen/'']
# To get LI_AT key
# Navigate to www.linkedin.com and log in
# Open browser developer tools (Ctrl-Shift-I or right click -> inspect element)
# Select the appropriate tab for your browser (Application on Chrome,Storage on Firefox)
# Click the Cookies dropdown on the left-hand menu,and select the www.linkedin.com option
# Find and copy the li_at value
myLI_AT_Key = ''INSERT LI_AT Key''
with ProfileScraper(cookie=myLI_AT_Key,scroll_increment = 50,scroll_pause = 0.8) as scraper:
    for link in my_profile_list:
        print(''Currently scraping: '',datetime.Now())
        profile = scraper.scrape(url=link)
        dataJSON = profile.to_dict()
        
        profileName = re.sub(''https://www.linkedin.com/in/'',link)
        profileName = profileName.replace("?originalSubdomain=es","")
        profileName = profileName.replace("?originalSubdomain=pe","")
        profileName = profileName.replace("?locale=en_US","")
        profileName = profileName.replace("?locale=es_ES","")
        profileName = profileName.replace("?originalSubdomain=uk","")
        profileName = profileName.replace("/","")
        
        with open(os.path.join(os.getcwd(),''w'') as json_file:
            json.dump(dataJSON,json_file)
            time.sleep(10)
            
print(''The first observation scraped was:'',my_profile_list[0:])
print(''The last observation scraped was:'',my_profile_list[-1:])
print(''END'')

注意事项:

代码略有不同,因为我在 SO here 上提出了一个问题,@Ananth 帮助我给出了解决方案。

我也知道在线和 SO 存在与 seleniumchromedriver 相关的“类似”问题,但在尝试了每个建议的解决方案后,我仍然无法使其正常工作。 (即常见的解决方案是 webdriver.Chrome() 中的拼写错误)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

我们今天的关于Selenium Webdriver单击Google搜索selenium打开谷歌浏览器的分享就到这里,谢谢您的阅读,如果想了解更多关于Java 如何强制Selenium WebDriver单击当前不可见的元素?、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、ruby – 无法使用Selenium WebDriver单击Safari中的按钮、Selenium WebDriver 在导入 selenium 时不可调用错误但在不导入 selenium 时有效的相关信息,可以在本站进行搜索。

本文标签: