猿问

如何使用 Selinium Python 获取 td web-table 中的文本

我正在尝试从 td webtable 中提取文本,但无法找到该元素;该表没有类或 ID,因此我尝试使用 xpath 但没有成功。


任何帮助是极大的赞赏。


driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check")


pair_1 = driver.find_element(By.XPATH("html/body/div[@id ='wrap']/div[@id='content-wrap']/div[@id='content']/div[contains(@class, ' ')]/div[contains(@class, ' ')]/div[contains(@class, 'ui-helper-resert')]/div[@id ='alignInfo']/div[@id ='alignments']/table/tbody/tr[2]/td[1]"))


print(pair_1.text)


#OR


pair_1.get_attribute("innerHTML")


print(pair_1)


返回以下错误


TypeError: 'str' object is not callable

更简单的 xpath


pair_1 = driver.find_element_by_xpath("//table/tbody/tr[2]/td[1]")

print(pair_1.text)

返回这个


Looking for [chromedriver 80.0.3987.06 mac6] driver in cache 

File found in cache by path [/Users/usr/.wdm/drivers/chromedriver/80.0.3987.06/mac6/chromedriver]

网站和 HTML

https://img1.sycdn.imooc.com/6537b81e000115ae06460699.jpg

慕的地6264312
浏览 127回答 3
3回答

慕雪6442864

尝试以下操作import timefrom selenium import webdriverdriver = webdriver.Chrome()driver.get("https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check")time.sleep(2)pair_1 = driver.find_element_by_css_selector(&nbsp; &nbsp; "#alignments > div:nth-child(1) > table > tbody > tr:nth-child(2) > td:nth-child(2)").get_attribute("innerHTML")print(pair_1)在 Windows 上,chrome 版本 80选择文本>右键单击>检查>右键单击>检查>右键单击<td>复制选择器

蝴蝶不菲

从Induce和()中得到“正向引物”、“反向引物”和“产物长度”的值。Primer pair 1WebDriverWait()visibility_of_element_locatedXPath1.创建一个项目列表,然后进行迭代。from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECproduct_list=['Forward primer','Reverse primer','Product length']driver=webdriver.Chrome()driver.get("https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check")for item in product_list:&nbsp; &nbsp; print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//h2[text()='Primer pair 1']/following::table[1]//th[text()='{}']/following-sibling::td[1]".format(item)))).text)输出:GAGGGCATACCCCTCGTAGACTGCCGTTTTCCGTAGGACT506要从所有引物对获取数据,请尝试此操作。1.创建引物列表,然后迭代。2.创建项目列表,然后进行迭代。from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECdriver=webdriver.Chrome()driver.get("https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check")list_primerpairs=['Primer pair 1','Primer pair 2','Primer pair 3','Primer pair 4','Primer pair 5']product_list=['Forward primer','Reverse primer','Product length']for primer in list_primerpairs:&nbsp; &nbsp; print(primer)&nbsp; &nbsp; print("===========================================")&nbsp; &nbsp; for item in product_list:&nbsp; &nbsp; &nbsp; &nbsp; print(WebDriverWait(driver,15).until(EC.visibility_of_element_located((By.XPATH,"//h2[text()='{}']/following::table[1]//th[text()='{}']/following-sibling::td[1]".format(primer,item)))).text)&nbsp; &nbsp; print("==========================================")输出:Primer pair 1===========================================GAGGGCATACCCCTCGTAGACTGCCGTTTTCCGTAGGACT506==========================================Primer pair 2===========================================AGTCCTACGGAAAACGGCAGGCTATTCTCGCAGCTCACCA621==========================================Primer pair 3===========================================AGGTAGTCAGTCAGGTCCCGTGCCGTTTTCCGTAGGACTC558==========================================Primer pair 4===========================================AGGGCATACCCCTCGTAGATTCTGCCGTTTTCCGTAGGAC506==========================================Primer pair 5===========================================GGGCATACCCCTCGTAGATGGCCGTTTTCCGTAGGACTCT502==========================================希望这可以帮助。

BIG阳

尝试以下代码:driver.get('https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check')str1 = "Primer pair 1";str2 = "Forward primer";el = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='" +str1 +"']//following-sibling::table//th[text()='" +str2+"']//following-sibling::td[1]")))print(el.text)您可以更改字符串值。出现此错误TypeError: 'str' object is not callable是因为您在这一行中出错:pair_1 = driver.find_element(By.XPATH("the value"))它应该:pair_1 = driver.find_element(By.XPATH, "the value")
随时随地看视频慕课网APP

相关分类

Html5
我要回答