所以有一些看起来像这样的HTML
<a class="" data-style-name="Black" data-style-id="16360" "true" data-description="null"<img width="32" height="32"
我想知道我是否可以获得文字&#34; Black&#34;从中而不是单击它,但是没有类名也循环通过,xpath不会返回任何内容
答案 0 :(得分:0)
a
被称为elements = driver.find_elements_by_xpath("//a[@data-style-name]")
for element in elements:
print element.get_attribute("data-style-name")
元素的属性,“黑色”是其值。
这是一种使用selenium&amp; amp;访问属性值的方法。蟒:
data-style-name
如果您只想选择属性为driver.find_elements_by_xpath("//a[@data-style-name=Black]")
且值为“黑色”的元素:
Container is running beyond physical memory limits. Current usage: 6.1 GB of 6 GB physical memory used; 10.8 GB of 30 GB virtual memory used.
有关xpath的更多信息:https://www.w3.org/TR/xpath/#section-Introduction
答案 1 :(得分:0)
您是否试过a_check = browser.find_element_by_xpath("/html/body/a[@data-style-name='Black']")
?
<selenium.webdriver.remote.webelement.WebElement (session="6c94ac24e0ec3a3320ec21b24055f4fa", element="0.1043557711542944-1")>
返回:
<script>
let body = document.querySelector('body');
body.id = 'logos';
let init = () => {
var myPS = new ParticleSlider({
slideDelay: 10,
mouseForce: 1000,
monochrome: false,
//showArrowControls: true,
ptlGap: 5,
ptlSize: 2,
arrowPadding: 15,
sliderId: 'logos',
});
(window.addEventListener
? window.addEventListener('click', function () {
myPS.init(true)
}, false)
: window.onclick = function () {
myPS.init(true)
});
window.onresize = () => {
myPS.width = window.innerWidth;
myPS.height = window.innerHeight;
}
}
window.onload = init;
</script>
</head>
<body id="logos">
<div class="slides">
<div id="first-slide" class="slide" data-src="https://www.brandsoftheworld.com/sites/default/files/styles/logo-thumbnail/public/0020/5527/brand.gif?itok=SOQ1ZybO">
</div>
</div>
<canvas class="draw"></canvas>
<script src="js/particleSlider.js"></script>