小编Deb*_*anB的帖子

Python Selenium - 检查一个 WebElement 是否等于另一个 WebElement

如何比较两个 selenium WebElements 以查看它们是否相同?

首先我检索一个列表input_fieldsfirst_input元素:

self.input_fields = driver.find_elements(By.CLASS_NAME, class_name) self.first_input = driver.find_element(By.ID, id)

然后我尝试检查input_fields[0]first_input是否是同一个 WebElement。

if self.first_input is not self.input_fields[0]:
    self.__log.warning("WebElement first_input : {} != {}".format(self.first_input, self.input_fields[0]))
Run Code Online (Sandbox Code Playgroud)

尽管sessionelement相同,但无论如何都会触发警告消息。

WARNING  - WebElement first_input: <selenium.webdriver.remote.webelement.WebElement (session="796bf0bcf3e0df528ee932d477951689", element="94a2ee62-9511-45e5-8aa3-bd3d3e9be309")> != <selenium.webdriver.remote.webelement.WebElement (session="796bf0bcf3e0df528ee932d477951689", element="94a2ee62-9511-45e5-8aa3-bd3d3e9be309")>
Run Code Online (Sandbox Code Playgroud)

python selenium webdriver getattribute selenium-webdriver

1
推荐指数
1
解决办法
1123
查看次数

WebDriverException:消息:长时间后无法访问 chrome

这是代码:

driver = webdriver.Chrome()
while True:
   #do thing that require hours
   #then i use selenium once
driver.get(link)
Run Code Online (Sandbox Code Playgroud)

我需要先打开硒,然后制作需要几个小时的东西,因为当我打开硒时,我需要做好准备并加快速度。如果放在driver = webdriver.Chrome()while 之下,它会减慢一切,我不知道它是否相关,但我用nohup命令运行此代码。

追溯:

Traceback (most recent call last):
  File "Scraper.py", line 84, in <module>
    main()
  File "Scraper.py", line 74, in main
    waitForSomething()
  File "Scraper.py", line 54, in waitForSomething
    fillForm(str(link)[2:-2])
  File "Scraper.py", line 30, in fillForm
    driver.get(link)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in …
Run Code Online (Sandbox Code Playgroud)

python selenium google-chrome selenium-chromedriver selenium-webdriver

1
推荐指数
1
解决办法
9614
查看次数

selenium.common.exceptions.WebDriverException:消息:无效参数:无法识别的功能:使用 Selenium 和 ChromeDriver 77.0 的 chromeOptions

这是我的代码

from selenium import webdriver
driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver')
driver.get('http://google.com')
Run Code Online (Sandbox Code Playgroud)

这是一条错误消息。

Traceback (most recent call last):
  File "D:/project/Python/TEST_selenium/chromedriver_test.py", line 16, in <module>
    driver = webdriver.Chrome(executable_path="C:\chromedriver_win32\chromedriver",chrome_options=chrome_options)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: unrecognized capability: chromeOptions
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚为什么在这个简单的代码中会出现错误。

这段代码在一个月前有效。但它现在不起作用。

我已经更新了 chrome 和 chrome 驱动程序,但这并不能解决问题。 …

python selenium google-chrome selenium-chromedriver selenium-webdriver

1
推荐指数
1
解决办法
2953
查看次数

在 Linux 上使用无头 Chrome 访问被拒绝的页面,而有头 Chrome 在 Windows 上使用 Selenium 通过 Python 工作

我有我在本地机器上使用的代码:

from selenium import webdriver
chrom_path = r"C:\Users\user\sof\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrom_path)
link = 'https://www.google.com/'
driver.get(link)
s = driver.page_source
print((s.encode("utf-8")))
driver.quit()
Run Code Online (Sandbox Code Playgroud)

并且此代码返回该网站的页面源,但是当我在Linux服务器centos7上使用此代码时:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver", chrome_options=options)
driver.get("https://www.google.com")
s = driver.page_source
print((s.encode("utf-8")))
driver.quit()
Run Code Online (Sandbox Code Playgroud)

这段代码也应该返回页面源代码,但这段代码返回:

b'<html><head>\n<title>Access Denied</title>\n</head><body>\n<h1>Access Denied</h1>\n \nYou don\'t have permission to access "http://www.newark.com/" on this server.<p>\nReference #18.456cd417.1576243477.e007b9f\n\n\n</p></body></html>'
Run Code Online (Sandbox Code Playgroud)

有人知道为什么相同的代码在不同的操作系统上的工作方式不同吗?

python selenium user-agent selenium-chromedriver google-chrome-headless

1
推荐指数
1
解决办法
3970
查看次数

如何在使用 Selenium Python 之前定位伪元素 ::before

我正在使用 Selenium Python 来定位标签元素。我想使用::before来定位它,因为这是一个弹出窗口。

 <div class="crow" grp="0" grpname="Pizza Size">
    ::before
    <label class="label0" cid="1">
    <input type="radio" name="0" coname="M" sname="" price="9.99" value="392">M<b class="ip">9.99</b>
    </label>
    <label class="label0" cid="1"><input type="radio" name="0" coname="L" sname="" price="11.99" value="393">L<b class="ip">11.99</b>
    </label><div style="clear:both">
    </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用::before来定位它,有朋友可以帮忙吗?

python selenium css-selectors pseudo-element selenium-webdriver

1
推荐指数
1
解决办法
4772
查看次数

java.lang.NoClassDefFoundError: org/openqa/selenium/WrapsElement 执行基于 Selenium 的 mvn 测试时出错

因此,我尝试使用运行测试,当我从 cmd 运行它时,mvn test -P它给了我,尽管它在 eclipse 本身中运行良好。java.lang.NoClassDefFoundError: org/openqa/selenium/WrapsElement

我有我的页面类src/main/java和我的测试src/test/java,我只是不明白为什么它在 Eclipse 中工作得很好,但在命令提示符下却不能。我认为它与 pom.xml 有关,但我不确定

这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>selenium</groupId>
<artifactId>taf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>taf</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${testNGSuite}</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <additionalClasspathElements>
                        <additionalClasspathElement>${project.basedir}/target/classes</additionalClasspathElement>
                    </additionalClasspathElements>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<dependencies>
    <dependency>
        <groupId>com.github.javafaker</groupId>
        <artifactId>javafaker</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId> …
Run Code Online (Sandbox Code Playgroud)

java selenium noclassdeffounderror maven selenium-webdriver

1
推荐指数
1
解决办法
1万
查看次数

java.net.ConnectException:无法通过 Spring Bot 中的 Selenium Java 使用 ChromeDriver Chrome 连接到 localhost/0:0:0:0:0:0:0:1:1731

我有使用 Spring Boot 开发的基于硒的 Web 应用程序。该服务器位于谷歌云服务器的虚拟机实例。

请在下面找到有关版本的详细信息。

Spring Boot - 2.0.0.RELEASE

硒 - 3.9.1

Linux - Debian 4.9.130-2 (2018-10-27) x86_64 GNU/Linux

Chrome 驱动程序 - 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881)

谷歌浏览器 - 70.0.3538.110

JDK - 1.8.0_232

从过去几天开始,我面临一个奇怪的问题,应用程序在营业时间开始时运行良好,但几个小时后变得不稳定,整个应用程序停止工作,甚至用户无法登录。在为此问题挖掘了更多信息后,我从服务器日志中发现了以下错误。我必须重新启动 VM 实例才能修复它。

在此处输入图片说明 .

重新启动后,它可以正常工作几个小时,然后我们再次面临同样的问题。

此外,我注意到在发送电子邮件时我们面临以下问题,该问题在几分钟前可以正常工作。 在此处输入图片说明

我还验证了服务器资源使用情况,但没有发现任何可疑活动。我在谷歌上搜索了这个问题,但大多数解决方案都是针对第一次运行时遇到的问题提供的。但是,就我而言,它可以正常工作几个小时,然后开始出现此问题。

请帮助我了解此问题的问题和根本原因。如果您也可以提出解决方案,那就更好了。

提前致谢。

java selenium google-chrome selenium-chromedriver spring-boot

1
推荐指数
1
解决办法
1326
查看次数

Selenium 驱动的 ChromeDriver 找不到 Chrome 二进制文件

我刚刚卸载了 Chrome,因为它表现得很奇怪(现已修复),之后 Python 中的 Selenium 无法识别 Chrome 驱动程序二进制文件,这非常奇怪,因为它应该完全不受影响,并且位于不同的位置和不同的版本从我在桌面上使用的 Chrome 来看,代码如下,并且已经工作多年了。

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--load-extension='+exension_path)
driver = webdriver.Chrome(executable_path=chrome_driver_folder,options=chrome_options)
Run Code Online (Sandbox Code Playgroud)

有人知道到底发生了什么事吗?我收到以下错误:

WebDriverException: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.18362 x86_64)
Run Code Online (Sandbox Code Playgroud)

python selenium google-chrome selenium-chromedriver selenium-webdriver

1
推荐指数
1
解决办法
2万
查看次数

如何使用 Python 抑制通过 Selenium 和 ChromeDriver 启动的 Brave 浏览器中的产品分析通知栏

我可以使用 Selenium、ChromeDriver 和 Python 启动 Brave 浏览器

代码试验:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

options = Options()
options.binary_location = r'C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe'
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.com/")
Run Code Online (Sandbox Code Playgroud)

但我无法摆脱几乎与Google Chrome通知栏类似的产品分析通知栏。

barve_product_analytics_notification_bar

谁能帮我吗?

python selenium google-chrome selenium-chromedriver brave-browser

1
推荐指数
1
解决办法
795
查看次数

如何使用 Selenium 在 #shadow-root (open) 中与 Cookie pop 交互

我想废弃 immowelt.de,但我无法通过 cookie 横幅。我尝试使用 sleep() 和 WebDriverWait 等待横幅加载,但是它们都不起作用。

这是网络驱动程序的代码

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

chrome_driver_path = '.../chromedriver'
url = 'https://www.immowelt.de/immobilienpreise'
driver = webdriver.Chrome(executable_path=chrome_driver_path)

driver.get(url)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="uc-center-container"]/div[2]/div/div/div/div[1]/button'))).click()

driver.close()
Run Code Online (Sandbox Code Playgroud)

这是带有睡眠的代码

from selenium import webdriver
import time
from selenium.webdriver.common.by import By

chrome_driver_path = '.../chromedriver'
url = 'https://www.immowelt.de/immobilienpreise'
driver = webdriver.Chrome(executable_path=chrome_driver_path)

driver.get(url)
time.sleep(5)
driver.find_element(By.XPATH, '//*[@id="uc-center-container"]/div[2]/div/div/div/div[1]/button').click()

driver.close()
Run Code Online (Sandbox Code Playgroud)

cookies selenium selenium-webdriver shadow-dom queryselector

1
推荐指数
1
解决办法
3688
查看次数