Reputation: 97
I am really new to selenium.
Currently, I am trying to use both selenium and beautifulsoup to do some webcrawling. The website that I am webcrawling on is https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp
.
this is the code that I have for now.
driver = webdriver.Chrome(executable_path=path_to_chromebrowser)
driver.get("https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp")
input_area = driver.find_element_by_name("searchForm.genename")
input_area.send_keys("P2RY12")
searcher = driver.find_element_by_class_name("button")
searcher.click()
# table = driver.find_element_by_class_name("table7 table7-border")
# table.find_element_by_tag_name("a").click()
I am trying to click the first SNP ID that comes up, upon search. What would be the good way for me to click the href of the search result?
Upvotes: 2
Views: 440
Reputation: 193058
ON the webpage https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp
to search for the Gene Name as P2RY12 and click the first SNP ID that comes up upon search you need to induce WebDriverWait for the element_to_be_clickable()
and you can use the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#idgname[name='searchForm.genename']"))).send_keys("P2RY12")
driver.find_element_by_css_selector("button.button[type='submit']").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form[action^='/dogsdv2/com/exportFile'] table>tbody>tr td:nth-child(3)>a"))).click()
Browser Snapshot:
Upvotes: 2
Reputation: 33384
To click on first link on the table induce WebDriverWait
() and element_to_be_clickable
() and following CSS selector.
Code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path=path_to_chromebrowser)
driver.get("https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp")
input_area = driver.find_element_by_name("searchForm.genename")
input_area.send_keys("P2RY12")
searcher = driver.find_element_by_class_name("button")
searcher.click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"table.table7.table7-border td>a[href^='/dogsdv2/refsnp/showRefSNPDetail']"))).click()
To get all the link induce WebDriverWait
() and visibility_of_all_elements_located
() and get the href value then iterate each url
allelemets=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"table.table7.table7-border td>a[href^='/dogsdv2/refsnp/showRefSNPDetail']")))
allurls=[item.get_attribute('href') for item in allelemets]
print(allurls)
for link in allurls:
driver.get(link)
Upvotes: 0
Reputation: 11
driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/form/table/tbody/tr[1]/td[3]/a[1]').click()
If you need other ids:
for id in range(1,10):
driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/form/table/tbody/tr[{}]/td[3]/a[1]'.format(id)).click()
sleep(5)
driver.back()
Upvotes: 0
Reputation: 357
Try this:
firstsnpID = driver.find_element_by_xpath("(.//table[@class='table7 table7-border']/tbody/tr/td[3]/a)[1]")
firstsnpID.click()
you can not use compound classes to locate element using find_element_by_class_name
Upvotes: 0