我的问题是关于使用dot和text()in的具体细节XPath.例如,以下find_element行返回相同的元素:
driver.get('http://stackoverflow.com/')
driver.find_element_by_xpath('//a[text()="Ask Question"]')
driver.find_element_by_xpath('//a[.="Ask Question"]')
Run Code Online (Sandbox Code Playgroud)
那么区别是什么呢?使用.和有text()什么好处和缺点?
例如,有一个列表
a=[1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
我可以用
a.append(some_value)
Run Code Online (Sandbox Code Playgroud)
在列表末尾添加元素,和
a.insert(exact_position, some_value)
Run Code Online (Sandbox Code Playgroud)
插入元素在列表中的任何其他位置,但不能在最后的
a.insert(-1, 5)
Run Code Online (Sandbox Code Playgroud)
将返回[1,2,3,5,4].那么如何使用list.insert(position,value)将元素添加到列表末尾?
我很好奇data参数和请求中的params参数之间的区别是什么python-requests,以及何时应该使用它们.
一个例子是我有一个dicts数组,users=[{"email_hash": "fh7834uifre8houi3f"}, ... ]我尝试用POST(requests.post())
params = {
"ads_token": blah blah,
"user_id": blah blah,
"users": json.dumps(users) # users=[{"email_hash": "fh7834uifre8houi3f"}, ... ]
"hash_type": "md5"
}
Run Code Online (Sandbox Code Playgroud)
因为users是几百长,所以得到的字符串json.dumps(users)(以及URL本身也是如此)很长,我得到了错误{'status_code': 414, 'reason': 'Request-URI Too Large'}.这是一个案例,data还是应该遵循一些其他途径?谢谢!
有重新创建文件夹的脚本:
# Remove folder (if exists) with all files
if os.path.isdir(str(os.path.realpath('..') + "\\my_folder")):
shutil.rmtree(os.path.realpath('..') + "\\my_folder", ignore_errors=True)
# Create new folder
os.mkdir(os.path.realpath('..') + "\\my_folder")
Run Code Online (Sandbox Code Playgroud)
这几乎总是有效,但在某些情况下(在创建步骤中),我得到了
WindowsError: [Error 5] Access is denied: 'C:\\Path\\To\\my_folder'
Run Code Online (Sandbox Code Playgroud)
什么可能导致此错误,我该如何避免?
我想自动执行文件下载完成检查chromedriver.
HTML下载列表中的每个条目看起来像
<a is="action-link" id="file-link" tabindex="0" role="link" href="http://fileSource" class="">DownloadedFile#1</a>
Run Code Online (Sandbox Code Playgroud)
所以我使用以下代码来查找目标元素:
driver.get('chrome://downloads/') # This page should be available for everyone who use Chrome browser
driver.find_elements_by_tag_name('a')
Run Code Online (Sandbox Code Playgroud)
这将返回空列表,同时有3个新下载.
正如我发现的那样,只能#shadow-root (open)处理标签的父元素.那么如何在这个#shadow-root元素中找到元素呢?
有一个目录包含文件夹以及不同格式的文件.
import os
my_list = os.listdir('My_directory')
Run Code Online (Sandbox Code Playgroud)
将返回文件和文件夹名称的完整内容.例如,我可以使用endswith('.txt')方法来选择文本文件名,但是如何获取只是文件夹名称的列表?
我正在尝试用 Python 编写一个刮刀以从页面中获取一些信息。就像这个页面上出现的优惠标题一样:https :
//www.justdial.com/Panipat/Saree-Retailers/nct-10420585
现在我使用这个代码:
import bs4
import requests
def extract_source(url):
source=requests.get(url).text
return source
def extract_data(source):
soup=bs4.BeautifulSoup(source)
names=soup.findAll('title')
for i in names:
print i
extract_data(extract_source('https://www.justdial.com/Panipat/Saree-Retailers/nct-10420585'))
Run Code Online (Sandbox Code Playgroud)
但是当我执行这段代码时,它给了我一个错误:
<titlee> Access Denied</titlee>
Run Code Online (Sandbox Code Playgroud)
我能做些什么来解决这个问题?
我无法在 chrome 中打开新标签页。我的要求是打开一个新选项卡做一些操作然后关闭这个新选项卡并返回到旧选项卡。下面的 python 代码在 Firefox 中有效,但在 Chrome 中无效。有人可以帮我吗?
ActionChains(driver).key_down(Keys.CONTROL,body).send_keys('t').key_up(Keys.CONTROL).perform()
Run Code Online (Sandbox Code Playgroud) 我有一个Python+ requests脚本.
脚本应该执行的步骤:
约束:
只能下载已批准的文件
我的代码:
requests.post(url_to_create, files={"file": open(path_to_file)})
requests.post(url_to_approve, data={'id': file_id})
requests.get(url_to_download, data={'id': file_id})
Run Code Online (Sandbox Code Playgroud)
问题:
这段代码几乎完美,但有时我没有文件.我发现第一个和第三个请求返回200状态代码而第二个返回202.据我所知(告诉我,如果我错了)状态202: Accepted意味着服务器接受请求并返回状态代码而没有实际请求完成
问题:
这是否意味着即使请求批准尚未完成也可以发送下载请求,如果是,那么在发送下载请求之前我怎么能等到批准请求完成?
我正在尝试创建一个跨浏览器的Python-Selenium测试脚本.因此无论我使用哪种webdriver(Chrome或IE),我都需要所有结果都相同.我可以设置浏览器窗口大小如下:
driver.set_window_size(1920, 1080)
Run Code Online (Sandbox Code Playgroud)
但是,下面的代码将用于返回不同的值Chrome和IE:
element = driver.find_element_by_xpath('some_xpath')
element.location
Run Code Online (Sandbox Code Playgroud)
尽管视窗区域(显示网页内容)大小不同(Chrome- 1910x998,IE- 1904x965),但窗口大小相同.为了获得我使用的这些值
driver.execute_script('return document.documentElement.clientHeight')
driver.execute_script('return document.documentElement.clientWidth')
Run Code Online (Sandbox Code Playgroud)
所以我试过了
driver.execute_script('document.documentElement.clientHeight = "990px";')
driver.execute_script('document.documentElement.clientWeight = "1900px";')
Run Code Online (Sandbox Code Playgroud)
但没有运气
那么问题是如何设置浏览器视口大小selenium?
python ×9
selenium ×4
list ×2
browser ×1
http ×1
insert ×1
javascript ×1
python-2.7 ×1
shadow-dom ×1
windowserror ×1
xpath ×1