[英]Trying to find a id in elements in python
I need help, 我需要帮助,
I'm trying to print the link from a website, Here is how the code looks like: 我正在尝试从网站上打印链接,这是代码的样子:
<div class="tabla_szoveg">
<div style="cursor:pointer" onclick="konyvjelzo('1523442');" class="torrent_konyvjelzo2"></div>
I'm trying to print the number inside "konyvjelzo('1523442');" 我正在尝试在“ konyvjelzo('1523442');”中打印数字。
Using selenium 使用硒
Also tried: 还尝试了:
linkgettr= driver.find_element_by_class("box_torrent_all")
but getting NONE Thanks 但是没有得到谢谢
Please check if torrent_konyvjelzo2
is dynamic ,if not then you can also replace below torrent
text with it otherwise you can use below code as it is. 请检查torrent_konyvjelzo2
是否是动态的,如果不是,那么也可以用torrent
文本替换下面的torrent
文本,否则可以直接使用以下代码。
split("'")[1] is used to split your konyvjelzo('1523442');
split(“'”)[1]用于拆分您的konyvjelzo('1523442');
text 文本
konyvjelzo( --> item 0
1523442 --> item 1
); --> item 2
the first item index starts from 0. so we can return 1 item. 第一个项目索引从0开始。因此我们可以返回1个项目。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'torrent')]")))
attribute=element.get_attribute("onclick")
print attribute.split("'")[1]
我认为您可以使用简单的正则表达式轻松找到它
ids = re.findall("onclick=\"konyvjelzo\('(.*?)'\);",page_text)
To print the partial value of onclick event ie 1523442 you have to induce WebDriverWait for the desired visibility_of_element_located()
and you can use the following solution: 要打印onclick事件的部分值 ,例如1523442,您必须为所需的visible_of_element_located visibility_of_element_located()
诱导WebDriverWait ,并且可以使用以下解决方案:
Using CSS_SELECTOR
: 使用CSS_SELECTOR
:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.tabla_szoveg>div.torrent_konyvjelzo2"))).get_attribute("onclick").split("'")[1])
Using XPATH
: 使用XPATH
:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[class='tabla_szoveg']/div[@class='torrent_konyvjelzo2']"))).get_attribute("onclick").split("'")[1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.