Reputation: 49
I will click that button in red circle.enter image description here
and I would like to crawling that site. How do I write python codes?
I tried this code and
from bs4 import BeautifulSoup
from urllib.request import urlopen
import time
from selenium import webdriver
driver = webdriver.Chrome('./chromedriver.exe')
url_base = 'https://www.kebhana.com/cont/mall/mall08/mall0805/index.jsp?_menuNo=62608'
driver.implicitly_wait(5)
driver.get(url_base)
openElement = driver.findElement(By.linkText("li[2]")).click();
time.sleep(2)
openElement.click()
time.sleep(5)
driver.quit()
soup
the error message were appeared like this:
AttributeError Traceback (most recent call last)
<ipython-input-16-19b58965022a> in <module>()
8
9 driver.get(url_base)
---> 10 openElement = driver.findElement(By.linkText("li[2]")).click();
11
12
AttributeError: 'WebDriver' object has no attribute 'findElement'
and the html code of that button is
<li class="on">
<a href="#none" onclick="javascript:doTab('spb_2812');">
<span>적 금</span>
</a>
</li>
Upvotes: 0
Views: 3935
Reputation: 193108
You need to take care of a couple of things:
As you are using Selenium-Python clients, findElement()
is not a valid line of code. Instead you need to use either of the following:
find_element_by_xpath()
find_element_by_css_selector()
linkText
accepts only text only.
time.sleep(5)
will degrade the Test Execution performance.To click()
on the element with text as 적 금
, you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.productSearchDiv li:nth-child(2) >a>span"))).click()
XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='productSearchDiv ']//li/a/span[text()='적 금']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Upvotes: 1
Reputation: 50864
findElement
is Java syntax, not Python. In addition, li
is a tag, not text, and by_link_text
doesn't work on <span>
tags anyway.
Use xpath
instead
driver.find_element_by_xpath('//li[.//span[.="적 금"]]')
Upvotes: 0
Reputation: 33384
To click on the tab you need to induce WebDriverWait
and element_to_be_clickable()
and use below xapth.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Chrome('./chromedriver.exe')
driver.get("https://www.kebhana.com/cont/mall/mall08/mall0805/index.jsp?_menuNo=62608")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='productSDiv']//li//a[contains(@onclick,'spb_2812')]"))).click()
Upvotes: 0