我有一个 Selenium 脚本(Python),它点击回复按钮使anonemail类出现。anonemail 类出现的时间各不相同。因此,我必须使用 sleep 直到元素出现。
我想等到课程出现而不是使用睡眠。我听说过等待命令,但我不知道如何使用它们。
这是我迄今为止所拥有的:
browser.find_element_by_css_selector(".reply-button").click()
sleep(5)
email=browser.find_element_by_css_selector(".anonemail").get_attribute("value")
Run Code Online (Sandbox Code Playgroud) python selenium selenium-webdriver webdriverwait expected-condition
我正在尝试从网站复制 href 值,html 代码如下所示:
<p class="sc-eYdvao kvdWiq">
<a href="https://www.iproperty.com.my/property/setia-eco-park/sale-
1653165/">Shah Alam Setia Eco Park, Setia Eco Park
</a>
</p>
Run Code Online (Sandbox Code Playgroud)
我试过了,driver.find_elements_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href")
但它回来了'list' object has no attribute 'get_attribute'
。使用driver.find_element_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href")
返回的None
. 但是我不能使用 xpath,因为该网站有 20+ 个 href,我需要全部复制。使用 xpath 只会复制一个。
如果有帮助,所有 20 多个 href 都归入同一类,即sc-eYdvao kvdWiq
.
最终,我想复制所有 20+ 个 href 并将它们导出到 csv 文件。
感谢任何可能的帮助。
我目前正在从事一个自动填写表格的项目。填写表单时会出现下一个按钮,这就是为什么它给我一个错误。
我试过了:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//input[@type='button' and @class='button']")))
Next = driver.find_element_by_xpath("//input[@type='button' and @class='button']")
Next.click()
Run Code Online (Sandbox Code Playgroud)
HTML:
<span class="btn">
<input type="button" value="Next" class="button" payoneer="Button" data-controltovalidate="PersonalDetails" data-onfieldsvalidation="ToggleNextButton" data-onclick="UpdateServerWithCurrentSection();" id="PersonalDetailsButton">
</input>
<div class="clearfix"></div>
</span>
Run Code Online (Sandbox Code Playgroud)
错误:
selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素在点 (203, 530) 处不可点击。其他元素将收到点击:...(会话信息:chrome=76.0.3809.132)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("https://hapondo.qa/rent/doha/apartments/studio")
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/head/title"))
)
print(element.text)
Run Code Online (Sandbox Code Playgroud)
在无头选项下无法获取页面标题?尝试过等待甚至尝试过driver.title
我有硒chromedriver的问题,我无法弄清楚是什么导致它.几周前一切正常,但突然出现这个错误.问题来自以下功能.
def login_(browser):
try:
browser.get("some_url")
# user credentials
user = browser.find_element_by_xpath('//*[@id="username"]')
user.send_keys(config('user'))
password = browser.find_element_by_xpath('//*[@id="password"]')
password.send_keys(config('pass'))
login = browser.find_element_by_xpath('/html/body/div[1]/div/button')
login.send_keys("\n")
time.sleep(1)
sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
sidebar.send_keys("\n")
app_submit = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/ul/li[1]/a')
app_submit.send_keys("\n")
except TimeoutException or NoSuchElementException:
raise LoginException
Run Code Online (Sandbox Code Playgroud)
此功能在开发环境(macOS 10.11)中没有问题,但在生产环境中引发以下错误:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="sidebar"]/ul/li[1]/a"}
(Session info: headless chrome=67.0.3396.79)
(Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee97XXX),platform=Linux 4.4.0-116-generic x86_64)
Run Code Online (Sandbox Code Playgroud)
我已经在每个环境中更新了Chrome和chromedriver(分别为v67和2.40).我也给了它更多time.sleep(15)
.但问题仍然存在.我最新的猜测是,webdriver的初始化可能无法正常工作:
def initiate_webdriver():
option = webdriver.ChromeOptions()
option.binary_location = config('GOOGLE_CHROME_BIN')
option.add_argument('--disable-gpu')
option.add_argument('window-size=1600,900')
option.add_argument('--no-sandbox')
if not config('DEBUG', cast=bool):
display = Display(visible=0, size=(1600, 900))
display.start() …
Run Code Online (Sandbox Code Playgroud) python selenium selenium-chromedriver selenium-webdriver webdriverwait
我需要帮助。有网址:https://www.inipec.gov.it/cerca-pec/-/pecs/companies。我需要点击复选框验证码:
我的代码看起来像:
import os, urllib.request, requests, datetime, time, random, ssl, json, codecs, csv, urllib
from urllib.request import Request, urlopen
from urllib.request import urlretrieve
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.options import Options
chromedriver = "chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chrome_options)
driver.get("https://www.inipec.gov.it/cerca-pec/-/pecs/companies")
driver.switch_to_default_content()
element = …
Run Code Online (Sandbox Code Playgroud) 我正在使用硒来抓取一些数据。
我点击的页面上有一个按钮说“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
我有一套Selenium测试,在我的本地环境中完美运行并使用Browserstack Automate,但在Azure DevOps上失败.
在Azure Devops上运行时,没有任何配置或设置更改.
我们已经按照这里的所有文档:https://docs.microsoft.com/en-us/azure/devops/pipelines/test/continuous-test-selenium?view=vsts
随机测试失败,从不相同.
由于超时,测试总是失败.我等待页面加载5分钟,所以不是超时的情况太低.
没有防火墙,应用程序是公开的.
身份验证始终成功,因此测试可以加载应用程序.
不知道下一步该尝试什么.
以下是Azure DevOps日志的副本.4个测试通过,但所有其他测试都失败了.通常,只有4-5次测试失败.
此测试使用BrowserStack Automate(远程selenium)和本地完美运行.
2018-11-17T05:40:28.6300135Z Failed StripeAdmin_WhenOnTab_DefaultSortIsByIdDescending
2018-11-17T05:40:28.6300461Z Error Message:
2018-11-17T05:40:28.6304198Z Test method CS.Portal.E2e.Tests.Admin.StripeAdmin.StripeAdminTests.StripeAdmin_WhenOnTab_DefaultSortIsByIdDescending threw exception:
2018-11-17T05:40:28.6305677Z OpenQA.Selenium.WebDriverTimeoutException: Timed out after 300 seconds
2018-11-17T05:40:28.6307041Z Stack Trace:
2018-11-17T05:40:28.6307166Z at OpenQA.Selenium.Support.UI.DefaultWait`1.ThrowTimeoutException(String exceptionMessage, Exception lastException)
2018-11-17T05:40:28.6307999Z at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
2018-11-17T05:40:28.6308188Z at CS.Portal.E2e.Tests.Utility.WebDriverUtilities.WaitForElement(IWebDriver driver, By by, Boolean mustBeDisplayed) in D:\a\1\s\CS.Portal.E2e.Tests\Utility\WebDriverUtilities.cs:line 26
2018-11-17T05:40:28.6319651Z at CS.Portal.E2e.Tests.Admin.StripeAdmin.StripeAdminTests.StripeAdmin_WhenOnTab_DefaultSortIsByIdDescending() in D:\a\1\s\CS.Portal.E2e.Tests\Admin\StripeAdmin\StripeAdminTests.cs:line 51
2018-11-17T05:40:28.6319982Z
2018-11-17T05:40:34.4671568Z Results File: D:\a\1\s\TestResults\VssAdministrator_factoryvm-az416_2018-11-17_03_08_24.trx
2018-11-17T05:40:34.4692222Z
2018-11-17T05:40:34.4695222Z Attachments:
2018-11-17T05:40:34.4697610Z D:\a\1\s\TestResults\672f4d28-5082-42e9-a7e7-f5645aadcfd8\VssAdministrator_factoryvm-az416 2018-11-17 03_02_43.coverage
2018-11-17T05:40:34.4697943Z …
Run Code Online (Sandbox Code Playgroud) 推特中的所有主题都可以在这个链接中找到 我想用里面的每个子类别抓取。
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
右侧的那些。
selenium ×10
webdriverwait ×10
python ×8
xpath ×3
web-scraping ×2
webdriver ×2
azure-devops ×1
c# ×1
java ×1
page-title ×1
recaptcha ×1