下面你可以看到我的代码。它运行一个项目列表,并提供一个表作为输出。
x = PrettyTable(["Soli", "Zusammenfassung", "Bearbeiter", "Status", "Termin"])
display = Display()
display.start()
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.example.com')
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from BeautifulSoup import BeautifulSoup
for j in range(0,len(total_tickets)):
url = driver.current_url
r = requests.get(url)
html_content = r.text
soup = BeautifulSoup(html_content, 'lxml')
ticket = driver.find_elements_by_xpath("//*[@id='ghx-issues-in-epic-table']/tbody/tr/td[2]/a")
ticket[j].click()
driver.get_screenshot_as_file("test.png")
worker = driver.find_element_by_xpath("//*[@id='peopledetails']/li/dl[1]/dd").find_element_by_class_name("user-hover").get_attribute("rel")
Soli = driver.find_element_by_xpath("//*[@id='key-val']").get_attribute("data-issue-key")
driver.find_element_by_xpath("//*[@id='summary-val']/span").click()
conclusion = driver.find_element_by_xpath("//*[@id='summary']").get_attribute("value")
status = soup.find('span',{'class':'classname'}).get_text
try:
termin = driver.find_element_by_xpath("//*[@id='datesmodule']").find_element_by_xpath("//*[@id='customfield_10090-val']/span[1]/time").get_attribute("datetime")
except NoSuchElementException:
termin = "No Deadline"
x.add_row([Soli, conclusion, worker, status, termin])
x.padding_width = 1
with open('file', 'w') as w:
w.write(str(x))
第一个问题,我收到此错误:
Traceback (most recent call last):
File "save.py", line 104, in <module>
status = soup.find('span',{'class':'classname'}).get_text
AttributeError: 'NoneType' object has no attribute 'get_text'
如果我删除“get_text”属性,状态部分总是显示“NONE”作为输出。
这是应该获取文本的HTML。我希望它向我展示span-tag之间的文本“NEU”。
<li class="item item-right">
<div class="wrap">
<strong class="name">
Status:
</strong>
<span id="status-val" class="value">
<span class="classname" original-title="">
Neu
</span>
</span>
<span class="status-view">(<a href="#" class="classname">Arbeitsablauf anzeigen</a>)
</span>
</div>
</li>
答案 0 :(得分:1)
你可以使用beautifulsoup中的select方法
soup.select("div#id")[0].text
[0]表示第一个元素
“#id”是div的id
.class是div的类
答案 1 :(得分:0)
如果您使用的是get_text
- 您需要将其称为it's a method:
soup.find('span',{'class':'classname'}).get_text()
将这个BeautifulSoup定位器“转换”为Selenium的更健壮和可靠的方法是 CSS选择器,而不是XPath - XPath不会对任何处理多值属性比如class
:
driver.find_element_by_css_selector("span.classname").text
或者,还有一种直接的“按类名”技术:
driver.find_element_by_class_name("classname").text
答案 2 :(得分:-1)
GOT IT:)
只需改变
status = soup.find('span',{'class':'classname'}).get_text
到
status = driver.find_element_by_xpath("xpath").text