0

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>

3 Answers 3

1

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.

  • Inducing time.sleep(5) will degrade the Test Execution performance.

Solution

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:

kebhana

Sign up to request clarification or add additional context in comments.

Comments

0

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()

1 Comment

@fairystarlight : Glad to help you.Please accept the answer by clicking on hollow under downvote button so It will turn green.
0

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[.="적 금"]]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.