小编Deb*_*anB的帖子

WebDriverWait 未按预期工作

我正在使用硒来抓取一些数据。

我点击的页面上有一个按钮说“custom_cols”。此按钮为我打开一个窗口,我可以在其中选择我的列。

这个新窗口有时需要一些时间才能打开(大约 5 秒)。所以为了处理这个我用过

WebDriverWait 
Run Code Online (Sandbox Code Playgroud)

延迟为 20 秒。但有时它无法在新窗口中选择查找元素,即使该元素可见。这种情况只有十次发生一次,其余时间它都可以正常工作。

我也在其他地方使用了相同的功能(WebDriverWait),它按预期工作。我的意思是它会等到元素可见,然后在找到它的那一刻点击它。

我的问题是为什么即使我正在等待元素可见,新窗口上的元素也不可见。要在这里添加,我试图增加延迟时间,但我仍然偶尔会遇到该错误。

我的代码在这里

def wait_for_elem_xpath(self, delay = None, xpath = ""):
    if delay is None:
        delay = self.delay

    try:
        myElem = WebDriverWait(self.browser, delay).until(EC.presence_of_element_located((By.XPATH , xpath)))
    except TimeoutException:
        print ("xpath: Loading took too much time!")
    return myElem
select_all_performance = '//*[@id="mks"]/body/div[7]/div[2]/div/div/div/div/div[2]/div/div[2]/div[2]/div/div[1]/div[1]/section/header/div'
self.wait_for_elem_xpath(xpath = select_all_performance).click()
Run Code Online (Sandbox Code Playgroud)

python selenium web-scraping webdriverwait expected-condition

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

如何在Selenium Java Client v3.11.0中删除超时和轮询时的弃用警告

下面是我的代码,在我将Selenium Webdriver版本更新为3.11.0之后,该代码显示为已弃用.

    private Wait<WebDriver> mFluentWait(WebDriver pDriver) {
    Wait<WebDriver> gWait = new FluentWait<WebDriver>(pDriver).withTimeout(100, TimeUnit.SECONDS)
            .pollingEvery(600, TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class);   
    return gWait;
}
Run Code Online (Sandbox Code Playgroud)

在代码中显示已弃用的警告withTimeoutpollingEvery部分.

如何重写此代码以便我可以删除已弃用的警告.

由于我是硒的新手,我不确定这种变化.任何帮助将不胜感激.

java selenium webdriver selenium-webdriver fluentwait

7
推荐指数
3
解决办法
6863
查看次数

如何通过Selenium的--user-data-dir参数打开Chrome配置文件

我正在尝试使用我现有的帐户和配置文件中的设置使用selenium加载chrome浏览器。
我可以使用ChromeOptions设置userdatadir和配置文件目录来使其工作。这会像我想要的那样用我的个人资料加载浏览器,但是浏览器随后挂起60秒钟并超时,而没有进行任何自动化操作。
如果我不使用用户数据目录和配置文件设置,则可以正常使用,但不使用我的配置文件。
我所做的阅读指出,一次不能使用相同的配置文件打开一个以上的浏览器,因此我确保在运行程序时没有打开任何文件。即使没有打开其他浏览器,它仍然挂起60秒。

m_Options = new ChromeOptions();
m_Options.AddArgument("--user-data-dir=C:/Users/Me/AppData/Local/Google/Chrome/User Data");
m_Options.AddArgument("--profile-directory=Default");
m_Options.AddArgument("--disable-extensions");
m_Driver = new ChromeDriver(@"pathtoexe", m_Options);
m_Driver.Navigate().GoToUrl("somesite");
Run Code Online (Sandbox Code Playgroud)

它始终挂在GoToUrl上。我不确定还有什么尝试。

c# selenium google-chrome selenium-chromedriver selenium-webdriver

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

严重:使用 Java 类版本 53.0 运行,但 Jenkins 需要 52.0 错误,通过命令提示符显示为“java -jar jenkins.war”

我是自动化工程师,我已经使用 maven 在 Jenkins 上部署了我的构建,我想在执行脚本时打开浏览器。当我尝试通过命令提示符运行 Jenkins 时,我搜索了一些人说我通过 cmd 提示符运行 Jenkins 战争文件:

java -jar Jenkins.war 
Run Code Online (Sandbox Code Playgroud)

然后系统产生如下错误:

SEVERE: Running with Java class version 53.0, but 52.0 is required error
Run Code Online (Sandbox Code Playgroud)

任何人都知道我该如何解决这个问题?

在此处输入图片说明

java jenkins

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

使用 Selenium 和 Python 查找存在 data-tb-test-id 属性而不是 id 的元素

我正在尝试使用 Selenium 查找元素,但没有找到。请遵循 HTML 代码:

\n\n
<div aria-disabled="false"\n     data-tb-test-id="DownloadCrosstab-Button"\n     role="button"\n     tabindex="0"\n     style="font-size: 12px; font-weight: normal; color: rgba(0, 0, 0, 0.7); display: inline-block; padding: 0px 24px; position: relative; text-align: center; border-style: solid; border-width: 1px; border-radius: 1px; height: 24px; line-height: 22px; min-width: 90px; box-sizing: border-box; outline: none; white-space: nowrap; user-select: none; cursor: default; background-color: rgba(0, 0, 0, 0); border-color: rgb(203, 203, 203); margin-top: 8px; width: 100%; -webkit-tap-highlight-color: transparent;"\n>Tabela de refer\xc3\xaancia cruzada</div>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我已经尝试过以下代码:

\n\n
x = browser.find_element_by_id("Downloadcrosstab")\nx = browser.find_element_by_link_text("Downloadcrosstab-Button")\nx = browser.find_element_by_class_name(\'Crosstab\')\n
Run Code Online (Sandbox Code Playgroud)\n\n …

python selenium xpath css-selectors webdriverwait

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

ElementClickInterceptedException:消息:元素点击被拦截:元素 &lt;label&gt; 不能用 Selenium 和 Python 点击

我试图单击“所有主题”和“所有状态”复选框,然后搜索结果。当我运行脚本时,会打开一个 1036x674 大小的 chrome 窗口。

如果我不理会窗口,我会收到元素点击拦截错误。如果我最小化或最大化窗口,我的脚本工作正常。

我正在使用 Selenium 3.141.0、chrome 76、chromedriver 76 和 python 3.6

chromedriver_path = r"C:\Users\path\to\chromedriver.exe"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx"

topics_xpath = "//*[@id=\"dnn_ctr81355_StateNetDB_UpdatePanel1\"]/div[1]/div[2]/span/label"
states_xpath = "//*[@id=\"dnn_ctr81355_StateNetDB_UpdatePanel1\"]/div[2]/div[2]/span/label"
browser.get(url)
time.sleep(30)

elem = browser.find_element_by_xpath(topics_xpath)
elem.click()
time.sleep(5)
elem = browser.find_element_by_xpath(states_xpath)
elem.click()
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

ElementClickInterceptedException:消息:元素点击被拦截:
元素 <label for="dnn_ctr81355_StateNetDB_ckBxAllTopics">...</label> 在点 (259, 665) 处不可点击。
其他元素将收到点击:
<label for="dnn_ctr81355_StateNetDB_ckBxTopics_0">...</label>
(会话信息:chrome=76.0.3809.100)

将被单击的复选框位于我要单击的复选框的正下方。

python selenium xpath css-selectors webdriverwait

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

使用 DataDome 的网站在使用 Selenium 和 Python 进行抓取时被验证码阻止

我实际上正在尝试从不同的网站上抓取一些汽车数据,我一直在 chromebrowser 中使用 selenium,但有些网站实际上通过验证码验证阻止了 selenium(例如: https: //www.leboncoin.fr/),这只是1 或 2 个请求。我尝试在 chromebrowser 中更改 $_cdc 但这并没有解决问题,并且我一直在 chromebrowser 中使用这些选项

user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
options = webdriver.ChromeOptions()
options.add_argument(f'user-agent={user_agent}')
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--profile-directory=Default')
options.add_argument("--incognito")
options.add_argument("--disable-plugins-discovery")
options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors", "safebrowsing-disable-download-protection", "safebrowsing-disable-auto-update", "disable-client-side-phishing-detection"])
options.add_argument('--disable-extensions')
browser = webdriver.Chrome(chrome_options=options)

browser.delete_all_cookies()

browser.set_window_size(800,800)

browser.set_window_position(0,0)
Run Code Online (Sandbox Code Playgroud)

我试图抓取的网站使用 DataDome 来保证机器人安全,有什么线索吗?

python selenium google-chrome web-scraping botdetect

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

在执行一段时间后,Selenium 为所有网站提供“从渲染器接收消息超时”

我有一个应用程序,我需要一个长时间运行的Selenium Web 驱动程序实例(我在无头模式下使用Chrome 驱动程序 83.0.4103.39)。基本上,该应用程序不断从队列中提取 url-data,并将提取的 url 提供给 Selenium,Selenium 应该在网站上执行一些分析。许多这些网站可能已关闭、无法访问或损坏,因此我将页面加载超时设置为 10 秒,以避免 Selenium 永远等待页面加载。
我在这里遇到的问题是,经过一些执行时间(假设 10 分钟)Selenium 开始给出Timed out receiving message from renderer每个 url 的错误。最初它工作正常,它可以正确打开好的网站并在坏网站上超时(网站无法加载),但一段时间后它开始对所有内容超时,即使是应该正确打开的网站(我已经检查过,它们在 Chrome 浏览器上正确打开)。我很难调试这个问题,因为应用程序中的每个异常都被正确捕获。我也注意到这个问题只发生在headless模式中。

  • 更新 *
    在网站分析期间,我还需要考虑 iframe(仅顶级),因此我还添加了一个逻辑来将驱动程序上下文切换到主页中的每个 iframe 并提取相关的 html。

这是应用程序的简化版本:

import traceback
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

width = 1024
height = 768

chrome_options = Options()
chrome_options.page_load_strategy …
Run Code Online (Sandbox Code Playgroud)

python iframe selenium renderer selenium-chromedriver

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

ElementClickInterceptedException:消息:元素单击被截获元素不可单击错误,使用 Selenium 和 Python 单击单选按钮

我正在尝试单击第一个框(ASN / DSD)

在此输入图像描述

但我收到此错误消息:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: 
Element <input type="radio" name="docTypes" ng-model="$ctrl.documentTypes.selected" id="documentType-0" ng-change="$ctrl.onChangeDocumentType()" ng-value="documentType" tabindex="0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]" aria-invalid="false"> 
is not clickable at point (338, 202). 
Other element would receive the click:
 <label translate-attr="{title: 'fulfillment.documentAction.createNew.modal.documentType.document.title'}" translate-values="{documentName: documentType.name}" for="documentType-0" translate="ASN - DSD" tabindex="0" title="Select ASN - DSD document type">...</label>
  (Session info: chrome=83.0.4103.116)
Run Code Online (Sandbox Code Playgroud)

我知道我已经输入了正确的 iframe,因为它可以找到该元素,只是不单击它。我的代码是

driver.switch_to.default_content()
iframes = driver.find_elements_by_tag_name("iframe")
driver.switch_to.frame(iframes[0])
time.sleep(5)
driver.find_element_by_xpath('//*[@id="documentType-0"]').click()
Run Code Online (Sandbox Code Playgroud)

我看到 DebanjanB 在这里回答了类似的问题:链接

我正在尝试使用执行脚本来完成他的第三个解决方案。我不知道该模型使用什么 CSS 选择器。模型看起来像这样

WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks")))
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='loadingWhiteBox']")))) …
Run Code Online (Sandbox Code Playgroud)

javascript python selenium webdriver webdriverwait

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

如何从推特上抓取所有主题

推特中的所有主题都可以在这个链接中找到 我想用里面的每个子类别抓取。

BeautifulSoup 在这里似乎没有用。我尝试使用 selenium,但我不知道如何匹配单击主类别后出现的 Xpath。

from selenium import webdriver
from selenium.common import exceptions

url = 'https://twitter.com/i/flow/topics_selector'
driver = webdriver.Chrome('absolute path to chromedriver')
driver.get(url)
driver.maximize_window()

main_topics = driver.find_elements_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div/span')

topics = {}
for main_topic in main_topics[2:]:
    print(main_topic.text.strip())
    topics[main_topic.text.strip()] = {}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用 来单击主类别main_topics[3].click(),但我不知道如何递归单击它们,直到我只找到Follow右侧的那些。

python selenium xpath web-scraping webdriverwait

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